index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * FeHelper 密码随机生成工具
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. number: true,
  8. lowerLetter: true,
  9. upperLetter: true,
  10. specialChar: false,
  11. length: 20,
  12. count: 1,
  13. chars: {
  14. number: '0123456789',
  15. lowerLetter: 'abcdefghijklmnopqrstuvwxyz',
  16. upperLetter: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  17. specialChar: '~!@#$%^&*()[{]}-_=+\|;:\'\",<.>/?`'
  18. },
  19. resultContent: '',
  20. showToast: false,
  21. toastMsg: ''
  22. },
  23. mounted: function () {
  24. this.loadPatchHotfix();
  25. },
  26. methods: {
  27. convert: function () {
  28. this.$nextTick(() => {
  29. let exceptedChars = ['number', 'lowerLetter', 'upperLetter', 'specialChar'].filter(item => this[item]).map(item => this.chars[item]).join('');
  30. // 生成指定数量的密码
  31. let passwords = [];
  32. for (let i = 0; i < this.count; i++) {
  33. let password = [], rands = [], rand = 0;
  34. for (let index = 0; index < this.length; index++) {
  35. // 尽可能不让字符重复
  36. do {
  37. rand = Math.floor(Math.random() * exceptedChars.length);
  38. } while (rands.includes(rand) && rands.length < exceptedChars.length);
  39. rands.push(rand);
  40. password.push(exceptedChars[rand]);
  41. }
  42. passwords.push(password.join(''));
  43. }
  44. this.resultContent = passwords.join('\n');
  45. });
  46. },
  47. getResult: function () {
  48. this.$refs.rstCode.select();
  49. },
  50. copyResult: function () {
  51. // 选中要复制的内容
  52. this.getResult();
  53. if ('clipboard' in navigator) {
  54. navigator.clipboard.writeText(this.resultContent)
  55. .then(() => {
  56. this.showToastMsg('复制成功!');
  57. })
  58. .catch(err => {
  59. console.error('复制失败: ', err);
  60. });
  61. }else{
  62. alert("您的浏览器不支持 clipboard API, 请手动复制")
  63. }
  64. },
  65. showToastMsg: function(msg) {
  66. this.toastMsg = msg;
  67. this.showToast = true;
  68. setTimeout(() => {
  69. this.showToast = false;
  70. }, 1500);
  71. },
  72. openOptionsPage: function(event) {
  73. event.preventDefault();
  74. event.stopPropagation();
  75. chrome.runtime.openOptionsPage();
  76. },
  77. openDonateModal: function(event) {
  78. event.preventDefault();
  79. event.stopPropagation();
  80. chrome.runtime.sendMessage({
  81. type: 'fh-dynamic-any-thing',
  82. thing: 'open-donate-modal',
  83. params: { toolName: 'password' }
  84. });
  85. },
  86. loadPatchHotfix() {
  87. // 页面加载时自动获取并注入页面的补丁
  88. chrome.runtime.sendMessage({
  89. type: 'fh-dynamic-any-thing',
  90. thing: 'fh-get-tool-patch',
  91. toolName: 'password'
  92. }, patch => {
  93. if (patch) {
  94. if (patch.css) {
  95. const style = document.createElement('style');
  96. style.textContent = patch.css;
  97. document.head.appendChild(style);
  98. }
  99. if (patch.js) {
  100. try {
  101. if (window.evalCore && window.evalCore.getEvalInstance) {
  102. window.evalCore.getEvalInstance(window)(patch.js);
  103. }
  104. } catch (e) {
  105. console.error('password补丁JS执行失败', e);
  106. }
  107. }
  108. }
  109. });
  110. },
  111. }
  112. });