No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

164 líneas
4.8 KiB

  1. import lesspass from 'lesspass';
  2. import {getDomainName} from './url-parser';
  3. function getStore(key, callback) {
  4. chrome.storage.local.get(key, result => {
  5. callback(result[key]);
  6. });
  7. }
  8. function saveStore(key, value, callback) {
  9. const newStore = {};
  10. newStore[key] = value;
  11. chrome.storage.local.set(newStore, () => {
  12. callback(value);
  13. });
  14. }
  15. function initStore(callback) {
  16. getStore('lesspassStore', store => {
  17. const defaultStore = Object.assign({
  18. login: '',
  19. counter: 1,
  20. password: {length: 12, settings: ['lowercase', 'uppercase', 'numbers', 'symbols']}
  21. }, store);
  22. saveStore('lesspassStore', defaultStore, store => {
  23. callback(store);
  24. });
  25. });
  26. }
  27. function getCurrentTab() {
  28. return new Promise(resolve => {
  29. chrome.tabs.query({active: true, currentWindow: true}, tabs => {
  30. resolve(tabs[0]);
  31. });
  32. });
  33. }
  34. function fillForm(data) {
  35. document.getElementById('loginField').value = data.login;
  36. document.getElementById('masterPasswordField').value = '';
  37. document.getElementById('siteField').value = data.site;
  38. document.getElementById('passwordCounter').value = data.counter;
  39. const passwordInfo = data.password;
  40. document.getElementById('passwordLength').value = passwordInfo.length;
  41. document.getElementById('lowercase').checked = false;
  42. document.getElementById('uppercase').checked = false;
  43. document.getElementById('numbers').checked = false;
  44. document.getElementById('symbols').checked = false;
  45. for (let i = 0; i < passwordInfo.settings.length; i++) {
  46. document.querySelector(`#${passwordInfo.settings[i]}`).checked = true;
  47. }
  48. }
  49. function selectGoodField() {
  50. const loginField = document.getElementById('loginField');
  51. const passwordField = document.getElementById('masterPasswordField');
  52. if (loginField.value === '') {
  53. loginField.focus();
  54. } else {
  55. passwordField.focus();
  56. }
  57. }
  58. function displayMessage(message, field) {
  59. const messageField = document.getElementById(field);
  60. messageField.textContent = message;
  61. }
  62. function getData() {
  63. const defaultOptions = {
  64. login: document.getElementById('loginField').value,
  65. counter: document.getElementById('passwordCounter').value,
  66. password: {
  67. length: document.getElementById('passwordLength').value,
  68. settings: []
  69. }
  70. };
  71. const options = ['lowercase', 'uppercase', 'numbers', 'symbols'];
  72. for (let i = 0; i < options.length; i++) {
  73. if (document.getElementById(options[i]).checked) {
  74. defaultOptions.password.settings.push(options[i]);
  75. }
  76. }
  77. return defaultOptions;
  78. }
  79. function getFormData() {
  80. const initData = getData();
  81. initData.masterPassword = document.getElementById('masterPasswordField').value;
  82. initData.site = document.getElementById('siteField').value;
  83. return initData;
  84. }
  85. document.getElementById('saveDefaultOptionButton').addEventListener('click', () => {
  86. const options = getData();
  87. getStore('lesspassStore', store => {
  88. const newStore = Object.assign(store, options);
  89. saveStore('lesspassStore', newStore, () => {
  90. displayMessage('(saved)', 'messageField');
  91. });
  92. });
  93. });
  94. document.getElementById('loginField').addEventListener('blur', generatePassword);
  95. document.getElementById('masterPasswordField').addEventListener('blur', generatePassword);
  96. document.getElementById('siteField').addEventListener('blur', generatePassword);
  97. function generatePassword() {
  98. const data = getFormData();
  99. if (!data.login || !data.masterPassword || !data.site) {
  100. return;
  101. }
  102. return lesspass.generatePassword(data.login, data.masterPassword, data.site, data).then(password => {
  103. document.getElementById('generatedPasswordField').value = password;
  104. return password;
  105. });
  106. }
  107. function copyPassword() {
  108. const generatedPasswordField = document.getElementById('generatedPasswordField');
  109. generatedPasswordField.disabled = false;
  110. generatedPasswordField.select();
  111. document.execCommand('copy');
  112. generatedPasswordField.disabled = true;
  113. }
  114. document.getElementById('loginButton').addEventListener('click', event => {
  115. event.preventDefault();
  116. const data = getFormData();
  117. if (!data.login || !data.masterPassword || !data.site) {
  118. displayMessage('login, master password and site are required to generate a password', 'errorMessageField');
  119. return;
  120. }
  121. const tabPromise = getCurrentTab();
  122. const passwordPromise = lesspass.generatePassword(data.login, data.masterPassword, data.site, data);
  123. Promise.all([passwordPromise, tabPromise]).then(values => {
  124. if (chrome.tabs && chrome.tabs.sendMessage) {
  125. chrome.tabs.sendMessage(values[1].id, {login: data.login, password: values[0]});
  126. window.close();
  127. } else {
  128. copyPassword();
  129. }
  130. });
  131. });
  132. chrome.tabs.query({active: true, currentWindow: true}, tabs => {
  133. if (tabs[0]) {
  134. initStore(store => {
  135. store.site = getDomainName(tabs[0].url);
  136. fillForm(store);
  137. selectGoodField();
  138. });
  139. }
  140. });