index.js 3.7 KB

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