index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * FeHelper Popup Menu
  3. */
  4. import Awesome from '../background/awesome.js'
  5. import MSG_TYPE from '../static/js/common.js';
  6. function triggerScreenshot() {
  7. chrome.tabs.query({active: true, currentWindow: true}, tabs => {
  8. if (!tabs || !tabs.length || !tabs[0].id) return;
  9. const tabId = tabs[0].id;
  10. // 先尝试直接发送消息给content script
  11. chrome.tabs.sendMessage(tabId, {
  12. type: 'fh-screenshot-start'
  13. }).then(response => {
  14. console.log('截图工具触发成功');
  15. window.close();
  16. }).catch(error => {
  17. console.log('无法直接触发截图工具,尝试使用noPage模式', error);
  18. // 如果发送消息失败,使用noPage模式
  19. chrome.runtime.sendMessage({
  20. type: 'fh-dynamic-any-thing',
  21. thing: 'trigger-screenshot',
  22. tabId: tabId
  23. });
  24. window.close();
  25. });
  26. });
  27. }
  28. new Vue({
  29. el: '#pageContainer',
  30. data: {
  31. manifest: {},
  32. fhTools: {}
  33. },
  34. created: function () {
  35. // 获取当前ctx的version
  36. this.manifest = chrome.runtime.getManifest();
  37. Awesome.getInstalledTools().then(tools => {
  38. this.fhTools = tools;
  39. });
  40. // 自动开关灯
  41. DarkModeMgr.turnLightAuto();
  42. // 记录工具使用
  43. // 埋点:自动触发json-format-auto
  44. chrome.runtime.sendMessage({
  45. type: 'fh-dynamic-any-thing',
  46. thing: 'statistics-tool-usage',
  47. params: {
  48. tool_name: 'popup'
  49. }
  50. });
  51. },
  52. mounted: function () {
  53. // 整个popup窗口支持上线选择
  54. document.body.addEventListener('keydown', e => {
  55. let keyCode = e.keyCode || e.which;
  56. if (![38, 40, 13].includes(keyCode)) {
  57. return false;
  58. }
  59. let ul = document.querySelector('#pageContainer ul');
  60. let hovered = ul.querySelector('li.x-hovered');
  61. let next, prev;
  62. if (hovered) {
  63. hovered.classList.remove('x-hovered');
  64. next = hovered.nextElementSibling;
  65. prev = hovered.previousElementSibling;
  66. }
  67. if (!next) {
  68. next = ul.querySelector('li:first-child');
  69. }
  70. if (!prev) {
  71. prev = ul.querySelector('li:last-child');
  72. }
  73. switch (keyCode) {
  74. case 38: // 方向键:↑
  75. prev.classList.add('x-hovered');
  76. break;
  77. case 40: // 方向键:↓
  78. next.classList.add('x-hovered');
  79. break;
  80. case 13: // 回车键:选择
  81. hovered.click();
  82. }
  83. }, false);
  84. // 查找截图按钮并绑定事件
  85. const screenshotButtons = Array.from(document.querySelectorAll('a[data-tool="screenshot"], button[data-tool="screenshot"]'));
  86. screenshotButtons.forEach(button => {
  87. // 移除原有的点击事件
  88. const oldClick = button.onclick;
  89. button.onclick = function(e) {
  90. e.preventDefault();
  91. e.stopPropagation();
  92. triggerScreenshot();
  93. return false;
  94. };
  95. });
  96. // 页面加载后自动采集
  97. if (window.chrome && chrome.runtime && chrome.runtime.sendMessage) {
  98. Awesome.collectAndSendClientInfo();
  99. }
  100. },
  101. methods: {
  102. runHelper: async function (toolName) {
  103. let request = {
  104. type: MSG_TYPE.OPEN_DYNAMIC_TOOL,
  105. page: toolName,
  106. noPage: !!this.fhTools[toolName].noPage
  107. };
  108. if(this.fhTools[toolName]._devTool) {
  109. request.page = 'dynamic';
  110. request.query = `tool=${toolName}`;
  111. }
  112. chrome.runtime.sendMessage(request);
  113. !!this.fhTools[toolName].noPage && setTimeout(window.close,200);
  114. },
  115. openOptionsPage: () => {
  116. chrome.runtime.openOptionsPage();
  117. },
  118. openUrl: function (event) {
  119. event.preventDefault();
  120. // 获取后台页面,返回window对象
  121. chrome.tabs.create({url: event.currentTarget.href});
  122. return false;
  123. }
  124. }
  125. });