Skip to content

Commit

Permalink
Release 0.0.6
Browse files Browse the repository at this point in the history
Добавлена поддержка Internet Explorer
  • Loading branch information
bankrot committed Sep 28, 2015
1 parent 5d25d03 commit cd40bdb
Show file tree
Hide file tree
Showing 12 changed files with 1,157 additions and 107 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ src/alt_cadesplugin_api.min.js

#### Зависимости

Для корректной работы скрипта необходима библиотека jQuery
Для корректной работы скрипта необходимы следующие библиотеки

1. [jQuery](https://github.com/jquery/jquery)
2. [es6-promise](https://github.com/jakearchibald/es6-promise)

#### Использование

Expand Down
8 changes: 6 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cadesplugin",
"version": "0.0.5",
"version": "0.0.6",
"homepage": "https://github.com/bankrot/cadesplugin",
"description": "CryptoPRO plugin api library (Крипто ПРО)",
"main": "src/alt_cadesplugin_api.js",
Expand All @@ -21,5 +21,9 @@
"Volkov Sergey"
],
"license": "MIT",
"ignore": []
"ignore": [],
"dependencies": {
"es6-promise": "~3.0.2",
"jquery": "~2.1.4"
}
}
6 changes: 5 additions & 1 deletion gulpfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ gulp.task 'main', (callback)->

# копирует jquery из node_modules
gulp.task 'libs', (callback)->
gulp.src ['./node_modules/jquery/dist/jquery.js', './node_modules/bowser/src/bowser.js']
gulp.src [
'./node_modules/jquery/dist/jquery.js'
'./node_modules/bowser/src/bowser.js'
'./node_modules/es6-promise/dist/es6-promise.js'
]
.pipe gulp.dest './test/js'

# запускает вебсервер для тестирования
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"devDependencies": {
"bowser": "^1.0.0",
"coffee-script": "^1.10.0",
"es6-promise": "^3.0.2",
"gulp": "^3.9.0",
"gulp-coffee": "^2.3.1",
"gulp-connect": "^2.2.0",
Expand Down
81 changes: 37 additions & 44 deletions src/alt_cadesplugin_api.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
###*
Библиотека для работы с плагином КриптоПРО
Версия 0.0.5 (beta)
Версия 0.0.6 (beta)
Поддерживает плагин версии 2.0.12245
Репозиторий https://github.com/bankrot/cadesplugin
###
Expand Down Expand Up @@ -48,13 +48,6 @@ AltCadesPlugin = class
###
timeout: 20000

###*
Нативные промисы поддерживаются
@property isPromise
@type {Boolean}
###
isPromise: not not window.Promise

###*
На основе webkit
@property isWebkit
Expand All @@ -63,6 +56,14 @@ AltCadesPlugin = class
isWebkit: do ->
return navigator.userAgent.match(/chrome/i) or navigator.userAgent.match(/opera/i)

###*
Internet Explorer
@property isIE
@type {Boolean}
###
isIE: do ->
return (navigator.appName is 'Microsoft Internet Explorer') or navigator.userAgent.match(/Trident\/./i)

###*
Конструктор
@method constructor
Expand Down Expand Up @@ -132,8 +133,8 @@ AltCadesPlugin = class
deferred = $.Deferred()

# обработчик события по загрузке плагина
listener = (event)=>
if event.data isnt 'cadesplugin_loaded'
$(window).on 'message', (event)=>
if event?.originalEvent?.data isnt 'cadesplugin_loaded'
return
setTimeout (->
cpcsp_chrome_nmcades.check_chrome_plugin (=>
Expand All @@ -144,7 +145,6 @@ AltCadesPlugin = class
deferred.reject message
)
), 0
window.addEventListener 'message', listener, false

# если через @timeout мс плагин все еще не вернул ответ, значит ошибка
setTimeout (=>
Expand All @@ -160,13 +160,7 @@ AltCadesPlugin = class
###
initNpapi: ->
deferred = $.Deferred()
if @isPromise
eventName = 'load'
else
eventName = 'message'
$(window).on eventName, (event)=>
if (not @isPromise) and (event.data isnt 'cadesplugin_echo_request')
return
$(window).on 'load', (event)=>
@loadNpapiPlugin()
@checked = true
result = @checkNpapiPlugin()
Expand All @@ -184,14 +178,10 @@ AltCadesPlugin = class
object = $ '<object id="cadesplugin_object" type="application/x-cades" style="visibility:hidden;"></object>'
$('body').append object
@pluginObject = object[0]
#if(isIE())
#{
#var elem1 = document.createElement('object');
#elem1.setAttribute("id", "certEnrollClassFactory");
#elem1.setAttribute("classid", "clsid:884e2049-217d-11da-b2a4-000e7bbb2b09");
#elem1.setAttribute("style", "visibility=hidden");
#document.getElementsByTagName("body")[0].appendChild(elem1);
#}

if @isIE
ieObject = $ '<object id="certEnrollClassFactory" classid="clsid:884e2049-217d-11da-b2a4-000e7bbb2b09" style="visibility=hidden"></object>'
$('body').append ieObject

###*
Проверяет плагин и возвращает true если проверка пройдена или строку с кодом ошибки
Expand All @@ -216,24 +206,20 @@ AltCadesPlugin = class

# Функция активации объектов КриптоПро ЭЦП Browser plug-in
createObject: (name)->
#if (isIE()) {
# // В Internet Explorer создаются COM-объекты
# if (name.match(/X509Enrollment/i)) {
# try {
# // Объекты CertEnroll создаются через CX509EnrollmentWebClassFactory
# var objCertEnrollClassFactory = document.getElementById("certEnrollClassFactory");
# return objCertEnrollClassFactory.CreateObject(name);
# }
# catch (e) {
# throw("Для создания обьектов X509Enrollment следует настроить веб-узел на использование проверки подлинности по протоколу HTTPS");
# }
#}
#// Объекты CAPICOM и CAdESCOM создаются обычным способом
#return new ActiveXObject(name);
#}
if @isIE
# В Internet Explorer создаются COM-объекты
if name.match /X509Enrollment/i
try
# Объекты CertEnroll создаются через CX509EnrollmentWebClassFactory
return $('certEnrollClassFactory')[0].CreateObject name
catch error
throw 'setup_https_for_x509enrollment'

# Объекты CAPICOM и CAdESCOM создаются обычным способом
return new ActiveXObject(name)

# В Firefox, Safari создаются объекты NPAPI
return @pluginObject.CreateObject(name)
return @pluginObject.CreateObject name

###*
Возвращает параметр из объекта
Expand Down Expand Up @@ -264,7 +250,7 @@ AltCadesPlugin = class

try
if typeof objectName is 'string'
result = @pluginObject.CreateObject objectName
result = @createObject objectName
if paramName
result = @extractParam result, paramName
else
Expand All @@ -282,7 +268,14 @@ AltCadesPlugin = class
###
extractParam: (object, param)->
if typeof param is 'object'
return object[param.method].apply object, param.args
# Здесь можно было бы использовать object[param.method].apply object, param.args
# Но в IE у внешних объектов нет матода .apply
switch param.args.length
when 0 then object[param.method]()
when 1 then object[param.method](param.args[0])
when 2 then object[param.method](param.args[0], param.args[1])
when 3 then object[param.method](param.args[0], param.args[1], param.args[2])
when 4 then object[param.method](param.args[0], param.args[1], param.args[2], param.args[3])
else
return object[param]

Expand Down
75 changes: 48 additions & 27 deletions src/alt_cadesplugin_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

/**
Библиотека для работы с плагином КриптоПРО
Версия 0.0.5 (beta)
Версия 0.0.6 (beta)
Поддерживает плагин версии 2.0.12245
Репозиторий https://github.com/bankrot/cadesplugin
*/
Expand Down Expand Up @@ -74,22 +74,24 @@ AltCadesPlugin = (function() {


/**
Нативные промисы поддерживаются
@property isPromise
На основе webkit
@property isWebkit
@type {Boolean}
*/

_Class.prototype.isPromise = !!window.Promise;
_Class.prototype.isWebkit = (function() {
return navigator.userAgent.match(/chrome/i) || navigator.userAgent.match(/opera/i);
})();


/**
На основе webkit
@property isWebkit
Internet Explorer
@property isIE
@type {Boolean}
*/

_Class.prototype.isWebkit = (function() {
return navigator.userAgent.match(/chrome/i) || navigator.userAgent.match(/opera/i);
_Class.prototype.isIE = (function() {
return (navigator.appName === 'Microsoft Internet Explorer') || navigator.userAgent.match(/Trident\/./i);
})();


Expand Down Expand Up @@ -179,13 +181,14 @@ AltCadesPlugin = (function() {
*/

_Class.prototype.initWebkit = function() {
var deferred, listener;
var deferred;
$.getScript('chrome-extension://iifchhfnnmpdbibifmljnfjhpififfog/nmcades_plugin_api.js');
window.postMessage('cadesplugin_echo_request', '*');
deferred = $.Deferred();
listener = (function(_this) {
$(window).on('message', (function(_this) {
return function(event) {
if (event.data !== 'cadesplugin_loaded') {
var ref;
if ((event != null ? (ref = event.originalEvent) != null ? ref.data : void 0 : void 0) !== 'cadesplugin_loaded') {
return;
}
return setTimeout((function() {
Expand All @@ -202,8 +205,7 @@ AltCadesPlugin = (function() {
})(this)));
}), 0);
};
})(this);
window.addEventListener('message', listener, false);
})(this));
setTimeout(((function(_this) {
return function() {
if (!_this.checked) {
Expand All @@ -221,19 +223,11 @@ AltCadesPlugin = (function() {
*/

_Class.prototype.initNpapi = function() {
var deferred, eventName;
var deferred;
deferred = $.Deferred();
if (this.isPromise) {
eventName = 'load';
} else {
eventName = 'message';
}
$(window).on(eventName, (function(_this) {
$(window).on('load', (function(_this) {
return function(event) {
var result;
if ((!_this.isPromise) && (event.data !== 'cadesplugin_echo_request')) {
return;
}
_this.loadNpapiPlugin();
_this.checked = true;
result = _this.checkNpapiPlugin();
Expand All @@ -254,10 +248,14 @@ AltCadesPlugin = (function() {
*/

_Class.prototype.loadNpapiPlugin = function() {
var object;
var ieObject, object;
object = $('<object id="cadesplugin_object" type="application/x-cades" style="visibility:hidden;"></object>');
$('body').append(object);
return this.pluginObject = object[0];
this.pluginObject = object[0];
if (this.isIE) {
ieObject = $('<object id="certEnrollClassFactory" classid="clsid:884e2049-217d-11da-b2a4-000e7bbb2b09" style="visibility=hidden"></object>');
return $('body').append(ieObject);
}
};


Expand Down Expand Up @@ -288,6 +286,18 @@ AltCadesPlugin = (function() {
};

_Class.prototype.createObject = function(name) {
var error, error1;
if (this.isIE) {
if (name.match(/X509Enrollment/i)) {
try {
return $('certEnrollClassFactory')[0].CreateObject(name);
} catch (error1) {
error = error1;
throw 'setup_https_for_x509enrollment';
}
}
return new ActiveXObject(name);
}
return this.pluginObject.CreateObject(name);
};

Expand Down Expand Up @@ -322,7 +332,7 @@ AltCadesPlugin = (function() {
} else {
try {
if (typeof objectName === 'string') {
result = this.pluginObject.CreateObject(objectName);
result = this.createObject(objectName);
if (paramName) {
result = this.extractParam(result, paramName);
}
Expand All @@ -347,7 +357,18 @@ AltCadesPlugin = (function() {

_Class.prototype.extractParam = function(object, param) {
if (typeof param === 'object') {
return object[param.method].apply(object, param.args);
switch (param.args.length) {
case 0:
return object[param.method]();
case 1:
return object[param.method](param.args[0]);
case 2:
return object[param.method](param.args[0], param.args[1]);
case 3:
return object[param.method](param.args[0], param.args[1], param.args[2]);
case 4:
return object[param.method](param.args[0], param.args[1], param.args[2], param.args[3]);
}
} else {
return object[param];
}
Expand Down
Loading

0 comments on commit cd40bdb

Please sign in to comment.