Advertisement
Guest User

Firefox Addon AutoAuth cancel Authentication PopUP

a guest
Feb 26th, 2015
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var AUTOAUTH = {
  2.     strings : {
  3.         _backup : null,
  4.         _main : null,
  5.  
  6.         initStrings : function () {
  7.             if (!this._backup) { this._backup = document.getElementById("autoauth-backup-bundle"); }
  8.             if (!this._main) { this._main = document.getElementById("autoauth-bundle"); }
  9.         },
  10.  
  11.         getString : function (key) {
  12.             this.initStrings();
  13.  
  14.             var rv = "";
  15.  
  16.             try {
  17.                 rv = this._main.getString(key);
  18.             } catch (e) {
  19.             }
  20.  
  21.             if (!rv) {
  22.                 try {
  23.                     rv = this._backup.getString(key);
  24.                 } catch (e) {
  25.                 }
  26.             }
  27.  
  28.             return rv;
  29.         },
  30.  
  31.         getFormattedString : function (key, args) {
  32.             this.initStrings();
  33.  
  34.             var rv = "";
  35.  
  36.             try {
  37.                 rv = this._main.getFormattedString(key, args);
  38.             } catch (e) {
  39.             }
  40.  
  41.             if (!rv) {
  42.                 try {
  43.                     rv = this._backup.getFormattedString(key, args);
  44.                 } catch (e) {
  45.                 }
  46.             }
  47.  
  48.             return rv;
  49.         }
  50.     },
  51.    
  52.     prefs : Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.autoAuth."),
  53.    
  54.     /*
  55.     get lastAuthJSON () {
  56.         var authText = AUTOAUTH.prefs.getCharPref("lastAuthJSON");
  57.        
  58.         try {
  59.             var authJSON = JSON.parse(authText);
  60.         } catch (e) { }
  61.        
  62.         if (!authJSON) {
  63.             authJSON = {};
  64.         }
  65.         else {
  66.             var now = Math.round(new Date().getTime() / 1000);
  67.            
  68.             for (i in authJSON) {
  69.                 if (now - authJSON[i] > 3) {
  70.                     delete authJSON[i];
  71.                 }
  72.             }
  73.            
  74.             AUTOAUTH.prefs.setCharPref("lastAuthJSON", JSON.stringify(authJSON));
  75.         }
  76.        
  77.         return authJSON;
  78.     },
  79.  
  80.     mobileLoad : function () {
  81.         removeEventListener("load", AUTOAUTH.mobileLoad, false);
  82.        
  83.         addEventListener("DOMWillOpenModalDialog", AUTOAUTH.mobileHandleEvent, true);
  84.        
  85.         addEventListener("unload", AUTOAUTH.mobileUnload, false);
  86.     },
  87.    
  88.     mobileUnload : function () {
  89.         removeEventListener("unload", AUTOAUTH.mobileUnload, false);
  90.        
  91.         removeEventListener("DOMWillOpenModalDialog", AUTOAUTH.mobileHandleEvent, true);
  92.     },
  93.    
  94.     mobileHandleEvent : function (e) {
  95.         switch (e.type) {
  96.             case "DOMWillOpenModalDialog":
  97.                 setTimeout(AUTOAUTH.mobileAutoAccept, 500);
  98.             break;
  99.         }
  100.     },
  101.    
  102.     mobileAutoAccept : function () {
  103.         var now = Math.round(new Date().getTime() / 1000);
  104.        
  105.         var authKey = btoa(document.getElementById("prompt-password-message").textContent);
  106.        
  107.         var lastAuthJSON = AUTOAUTH.lastAuthJSON;
  108.        
  109.         if (authKey in lastAuthJSON && (now - lastAuthJSON[authKey] <= 3)) {
  110.             return;
  111.         }
  112.        
  113.         if (document.getElementById("prompt-password-dialog")) {
  114.             if ((document.getElementById("prompt-password-user").value != '') && (document.getElementById("prompt-password-password").value != '')){
  115.                 lastAuthJSON[authKey] = now;
  116.                 AUTOAUTH.prefs.setCharPref("lastAuthJSON", JSON.stringify(lastAuthJSON));
  117.                
  118.                 document.getElementById("cmd_ok").doCommand();
  119.             }
  120.         }
  121.     }, */
  122.  
  123.     load : function () {
  124.         setInterval(clearPopup(), 1000);
  125.     },
  126.    
  127.     log : function (msg) {
  128.         var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
  129.         consoleService.logStringMessage("AUTOAUTH: " + msg);
  130.     }/*,
  131.    
  132.     fillIn : function (username, password) {
  133.         document.getElementById("loginTextbox").value = username;
  134.         document.getElementById("password1Textbox").value = password;
  135.         document.getElementById("checkbox").checked = true;
  136.         onCheckboxClick(document.getElementById("checkbox"));
  137.        
  138.         commonDialogOnAccept();
  139.         window.close();
  140.     }*/
  141. };
  142.  
  143. function clearPopup () {
  144.    
  145.         removeEventListener("load", AUTOAUTH.load, false);
  146.        
  147.         // var now = Math.round(new Date().getTime() / 1000);
  148.        
  149.         try {
  150.             var args = window.arguments[0].QueryInterface(Ci.nsIWritablePropertyBag2).QueryInterface(Ci.nsIWritablePropertyBag);
  151.             var authKey = args.getProperty("text");
  152.            
  153.             if (args.getProperty("promptType") != "promptUserAndPass") {
  154.                 return;
  155.             }
  156.         } catch (e) {
  157.             // Firefox < 4
  158.             var args = window.arguments[0].QueryInterface(Ci.nsIDialogParamBlock);
  159.             var authKey = args.GetString(0);
  160.                        
  161.             if (args.GetInt(3) != 2 || args.GetInt(4) == 1) {
  162.                 return;
  163.             }
  164.         }
  165.        
  166.        
  167.         /*
  168.         var lastAuthJSON = AUTOAUTH.lastAuthJSON;
  169.        
  170.         if (authKey in lastAuthJSON && (now - lastAuthJSON[authKey] <= 3)) {
  171.             return;
  172.         }
  173.         */
  174.        
  175.         if ((document.getElementById("loginTextbox").getAttribute("value") == '') || (document.getElementById("password1Textbox").getAttribute("value") == '')){
  176.            
  177.             /*
  178.             lastAuthJSON[authKey] = now;
  179.             AUTOAUTH.prefs.setCharPref("lastAuthJSON", JSON.stringify(lastAuthJSON));
  180.             */
  181.            
  182.             if (typeof commonDialogOnAccept != 'undefined') {
  183.                 commonDialogOnAccept();
  184.             }
  185.             else {
  186.                 document.getElementById("commonDialog").cancelDialog();
  187.             }
  188.            
  189.             window.close();
  190.         }
  191.         /*
  192.         else {
  193.             var passwordManager = Components.classes["@mozilla.org/passwordmanager;1"];
  194.  
  195.             if (passwordManager != null) {
  196.                 var matches = document.getElementById("info.box").getElementsByTagName("description")[0].firstChild.nodeValue.match(/(https?):\/\/([a-z0-9\.-]+)(:([0-9]+))?$/i);
  197.                 var protocol = matches[1];
  198.                 var host = matches[2];
  199.                 var port = matches[4];
  200.  
  201.                 if (!port) {
  202.                     if (protocol == 'https') {
  203.                         port = 443;
  204.                     } else {
  205.                         port = 80;
  206.                     }
  207.                 }
  208.  
  209.                 var possibleMatches = [];
  210.                 var hostREs = [];
  211.  
  212.                 var hostParts = host.split('.');
  213.                 hostParts.shift();
  214.  
  215.                 while (hostParts.length > 1) {
  216.                     var hostRE = hostParts.join("\.");
  217.                     hostRE = hostRE.replace(/-/g, "\-");
  218.  
  219.                     hostREs.push(hostRE);
  220.  
  221.                     hostParts.shift();
  222.                     possibleMatches.push([]);
  223.                 }
  224.  
  225.                 passwordManager = passwordManager.createInstance();
  226.                 passwordManager.QueryInterface(Components.interfaces.nsIPasswordManager);
  227.  
  228.                 var enumerator = passwordManager.enumerator;
  229.                 var nextPassword, host, username, password;
  230.                 var showList = false;
  231.  
  232.                 passwordEntries : while(enumerator.hasMoreElements()) {
  233.                     try {
  234.                         nextPassword = enumerator.getNext().QueryInterface(Components.interfaces.nsIPassword);
  235.  
  236.                         if (nextPassword.host.indexOf("://") == -1){
  237.                             for (var i = 0; i < hostREs.length; i++){
  238.                                 if (nextPassword.host.match(hostREs[i])){
  239.                                     var host = nextPassword.host;//.split('//')[1];
  240.                                     possibleMatches[i].push({ 'username' : nextPassword.user, 'password' : nextPassword.password, 'host' : host.split(" ")[0] });
  241.                                     var showList = true;
  242.                                     continue passwordEntries;
  243.                                 }
  244.                             }
  245.                         }  
  246.                     } catch (e) {
  247.                     }
  248.                 }
  249.                
  250.                 if (showList) {
  251.                     var box = document.createElement("hbox");
  252.  
  253.                     var button = document.createElement("button");
  254.                     button.setAttribute("label", AUTOAUTH.strings.getString("autoauth.autoFillWith"));
  255.                     button.setAttribute("onclick",'AUTOAUTH.fillIn(document.getElementById("autoauth-list").selectedItem.username, document.getElementById("autoauth-list").selectedItem.password);');
  256.  
  257.                     var list = document.createElement("menulist");
  258.                     list.setAttribute("id","autoauth-list");
  259.                     var popup = document.createElement("menupopup");
  260.                     var done = false;
  261.                
  262.                     for (var i = 0; i < possibleMatches.length; i++){
  263.                         for (var j = 0; j < possibleMatches[i].length; j++){
  264.                             var item = document.createElement("menuitem");
  265.                             item.setAttribute("label", possibleMatches[i][j].username + "@" + possibleMatches[i][j].host);
  266.                             item.username = possibleMatches[i][j].username;
  267.                             item.password = possibleMatches[i][j].password;
  268.    
  269.                             popup.appendChild(item);
  270.                        
  271.                             done = true;
  272.                         }
  273.                    
  274.                         if (done) break;
  275.                     }
  276.  
  277.                     list.appendChild(popup);
  278.                     box.appendChild(button);
  279.                     box.appendChild(list);
  280.  
  281.                     document.getElementById("loginContainer").parentNode.appendChild(box);
  282.                 }
  283.             }
  284.         }
  285.         */
  286.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement