/trunk/LockCrypt.FireFox/LockCrypt/chrome/content/options.js

# · JavaScript · 125 lines · 117 code · 3 blank · 5 comment · 14 complexity · 027f7c541a498725abbe532aa0b6ece1 MD5 · raw file

  1. var lcff_options = {
  2. onLoad : function() {
  3. LockCrypt.loadPrefs();
  4. document.getElementById("txtExcluded").value = LockCrypt.excludeList.join("\n");
  5. var passwordManager = Components.classes["@mozilla.org/login-manager;1"]
  6. .getService(Components.interfaces.nsILoginManager);
  7. var numLogins = passwordManager.countLogins("","","");
  8. dump(numLogins+" logins\n");
  9. document.getElementById("lblNumPasswords").value = LockCrypt.getFormattedString("numPasswordsFirefox", [ numLogins ]);
  10. },
  11. saveOptions : function() {
  12. var excludes = document.getElementById("txtExcluded").value;
  13. var excludeAry = [];
  14. var tempAry = excludes.split("\n");
  15. for(var i=0;i<tempAry.length;i++) {
  16. var currentExclude = tempAry[i].trim();
  17. if(currentExclude.length > 0) {
  18. excludeAry[excludeAry.length] = tempAry[i];
  19. }
  20. }
  21. LockCrypt.prefs.setCharPref("excludeList", JSON.stringify(excludeAry));
  22. close();
  23. },
  24. importFromLockCrypt : function() {
  25. var listener = {
  26. finished : function(data) {
  27. var responseData = JSON.parse(data);
  28. if(typeof responseData.error != 'undefined') {
  29. alert(decodeURIComponent(responseData.error));
  30. } else {
  31. var passwordManager = Components.classes["@mozilla.org/login-manager;1"]
  32. .getService(Components.interfaces.nsILoginManager);
  33. var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
  34. Components.interfaces.nsILoginInfo,
  35. "init");
  36. var accounts = responseData.accounts;
  37. var existingLogins;
  38. var thisExistingLogin;
  39. var accountExists;
  40. var thisAccount;
  41. var hostname;
  42. var numImported = 0;
  43. for(var i=0;i<accounts.length;i++) {
  44. accountExists = false;
  45. thisAccount = accounts[i];
  46. hostname = decodeURIComponent(thisAccount.hostname);
  47. //dump("Account for: "+hostname+"\n");
  48. existingLogins = passwordManager.findLogins({}, hostname, null, hostname);
  49. for(var j=0;j<existingLogins.length;j++) {
  50. thisExistingLogin = existingLogins[j];
  51. //dump("Hostname exists, type is ");
  52. //dump(thisExistingLogin+" existing username is ");
  53. //dump(thisExistingLogin.username+", new username is ");
  54. //dump(thisAccount.username+"\n");
  55. if(thisExistingLogin.username == thisAccount.username) {
  56. accountExists = true;
  57. break;
  58. }
  59. }
  60. if(!accountExists) {
  61. try{
  62. passwordManager.addLogin(new nsLoginInfo(hostname,
  63. null,
  64. hostname,
  65. decodeURIComponent(thisAccount.username),
  66. decodeURIComponent(thisAccount.password),
  67. decodeURIComponent(thisAccount.usernameField),
  68. decodeURIComponent(thisAccount.passwordField)));
  69. numImported++;
  70. } catch(ex) {
  71. dump("#"+numExported+": "+ex+"\n");
  72. }
  73. }
  74. }
  75. document.getElementById("transferProgress").hidden = true;
  76. alert(LockCrypt.getFormattedString("numImported", [numImported]));
  77. var numLogins = passwordManager.countLogins("","","");
  78. document.getElementById("lblNumPasswords").value = LockCrypt.getFormattedString("numPasswordsFirefox", [ numLogins ]);
  79. }
  80. }
  81. };
  82. document.getElementById("transferProgress").hidden = false;
  83. var data = {
  84. "type" : "EXPORT" // import from Firefox, but export to LockCrypt
  85. };
  86. LockCrypt.sendReceive('localhost',1556,JSON.stringify(data),listener);
  87. },
  88. exportToLockCrypt : function() {
  89. var listener = {
  90. finished : function(data) {
  91. document.getElementById("transferProgress").hidden = true;
  92. var responseData = JSON.parse(data);
  93. if(typeof responseData.error != 'undefined') {
  94. alert(decodeURIComponent(responseData.error));
  95. } else {
  96. alert(decodeURIComponent(responseData.response));
  97. }
  98. }
  99. };
  100. document.getElementById("transferProgress").hidden = false;
  101. var passwordManager = Components.classes["@mozilla.org/login-manager;1"]
  102. .getService(Components.interfaces.nsILoginManager);
  103. var logins = passwordManager.getAllLogins({});
  104. var pass;
  105. var accounts = [];
  106. for(var i=0;i<logins.length;i++) {
  107. pass = logins[i];
  108. accounts[accounts.length] = {
  109. url : encodeURIComponent(pass.hostname),
  110. username : encodeURIComponent(pass.username),
  111. usernameID : encodeURIComponent(pass.usernameField),
  112. password : encodeURIComponent(pass.password),
  113. passwordID : encodeURIComponent(pass.passwordField)
  114. };
  115. }
  116. var data = {
  117. "type" : "IMPORT", // export from Firefox, but import to LockCrypt
  118. "accounts" : accounts
  119. };
  120. LockCrypt.sendReceive('localhost',1556,JSON.stringify(data),listener);
  121. }
  122. };