function rutokenwebLoad(token) { var plugin; rutokenweb.ready .then(function () { if ($.browser.chrome) { return rutokenweb.isExtensionInstalled(); } else { return Promise.resolve(true); } }) .then(function (result) { if (result) { return rutokenweb.isPluginInstalled(); } else { throw "Rutoken Web Extension wasn't found"; } }) .then(function (result) { if (result) { return rutokenweb.loadPlugin(); } else { throw "Rutoken Web Plugin wasn't found"; } }) .then(function (pluginPromised) { plugin = pluginPromised; plugin .rtwIsTokenPresentAndOK() .then(function (isTokenPresent) { if (isTokenPresent === true) { return plugin.rtwGetNumberOfContainers(); } else { throw "Подключите USB-токен!"; } }) .then(function (containers_count) { var promises = []; for (var i = 0; i < containers_count; i++) { promises.push(plugin.rtwGetContainerName(i)); } return Promise.all(promises); }) .then(function (containers_names) { containers_names.forEach(function (cont_name) { domain = explode("#%#", cont_name); /*if (domain[1] == location.hostname) {*/ var nocache = Math.random(), rd = Math.random().toString(16); (clrnd = Sha256.hash(rd)), (random_text = ""), (user = domain[0]); $.get( "//" + location.hostname + "/rutoken/?tlogin=" + encodeURI(user) + "&nocache=" + nocache, function (data) { var random_text = Sha256.hash( clrnd + ":" + data ); plugin .rtwSign( cont_name, random_text, undefined ) .then(function (res) { if (res != -7 && res != -12) { if (res < 0) { console.log(res); } else { console.log(); message( token, "rutoken", ircodeParam( "tlogin", user ) + ircodeParam( "random", clrnd ) + ircodeParam( "tsign", res ) ); } } }) .then(undefined, function (reason) { console.log(reason); }); } ); /*}*/ }); }) .then(undefined, function (reason) { console.log(reason); }); }) .then(function (wrappedPlugin) {}) .then(undefined, function (reason) { console.log(reason); }); }