index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. methods: {
  23. convert: function () {
  24. this.$nextTick(() => {
  25. let exceptedChars = ['number', 'lowerLetter', 'upperLetter', 'specialChar'].filter(item => this[item]).map(item => this.chars[item]).join('');
  26. let password = [], rands = [], rand = 0;
  27. for (let index = 0; index < this.length; index++) {
  28. // 尽可能不让字符重复
  29. do {
  30. rand = Math.floor(Math.random() * exceptedChars.length);
  31. } while (rands.includes(rand) && rands.length < exceptedChars.length);
  32. rands.push(rand);
  33. password.push(exceptedChars[rand]);
  34. }
  35. this.resultContent = password.join('');
  36. });
  37. },
  38. getResult: function () {
  39. this.$refs.rstCode.select();
  40. },
  41. copyResult: function () {
  42. // 选中要复制的内容
  43. this.getResult();
  44. if ('clipboard' in navigator) {
  45. navigator.clipboard.writeText(this.resultContent)
  46. .then(() => {
  47. this.showToastMsg('复制成功!');
  48. })
  49. .catch(err => {
  50. console.error('复制失败: ', err);
  51. });
  52. }else{
  53. alert("您的浏览器不支持 clipboard API, 请手动复制")
  54. }
  55. },
  56. showToastMsg: function(msg) {
  57. this.toastMsg = msg;
  58. this.showToast = true;
  59. setTimeout(() => {
  60. this.showToast = false;
  61. }, 1500);
  62. },
  63. openOptionsPage: function(event) {
  64. event.preventDefault();
  65. event.stopPropagation();
  66. chrome.runtime.openOptionsPage();
  67. },
  68. openDonateModal: function(event) {
  69. event.preventDefault();
  70. event.stopPropagation();
  71. chrome.runtime.sendMessage({
  72. type: 'fh-dynamic-any-thing',
  73. thing: 'open-donate-modal',
  74. params: { toolName: 'password' }
  75. });
  76. }
  77. }
  78. });