index.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * FeHelper Content Scripts Manager
  3. * @author zhaoxianlie
  4. */
  5. // Tarp.require.config初始化
  6. (() => {
  7. let absPath = (() => {
  8. let stack;
  9. try {
  10. a.b();
  11. }
  12. catch (e) {
  13. stack = e.fileName || e.sourceURL || e.stack || e.stacktrace;
  14. }
  15. if (stack) {
  16. return /((?:http|https|file|chrome-extension):\/\/.*?\/[^:]+)(?::\d+)?:\d+/.exec(stack)[1];
  17. }
  18. })();
  19. Tarp.require.config = {
  20. paths: [absPath],
  21. uri: absPath
  22. };
  23. })();
  24. // JSON页面自动格式化,事件注册
  25. chrome.runtime.sendMessage({
  26. type: MSG_TYPE.JSON_PAGE_FORMAT_REQUEST
  27. });
  28. // js、css页面自动格式化,事件注册
  29. chrome.runtime.sendMessage({
  30. type: MSG_TYPE.JS_CSS_PAGE_BEAUTIFY_REQUEST
  31. });
  32. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  33. chrome.runtime.onMessage.addListener(function (request, sender, callback) {
  34. switch (request.type) {
  35. // JSON页面自动格式化
  36. case MSG_TYPE.JSON_PAGE_FORMAT:
  37. request.canIDoIt && Tarp.require('../json-format/automatic', true).then(JsonTools => JsonTools.format());
  38. break;
  39. // js、css页面自动检测,提示格式化
  40. case MSG_TYPE.JS_CSS_PAGE_BEAUTIFY:
  41. request.canIDoIt && Tarp.require('../code-beautify/automatic', true).then(beautifier => beautifier.detect());
  42. break;
  43. // 二维码解码
  44. case MSG_TYPE.QR_DECODE:
  45. Tarp.require('../qr-code/decode', true).then(qrcode => qrcode.show(request.result));
  46. break;
  47. // 全屏截图
  48. case MSG_TYPE.PAGE_CAPTURE_SCROLL:
  49. Tarp.require('../page-capture/index', true).then(page => page.scroll(callback));
  50. break;
  51. // 页面性能检测
  52. case MSG_TYPE.GET_PAGE_WPO_INFO:
  53. Tarp.require('../wpo/inject', true).then(wpo => {
  54. (function check() {
  55. (document.readyState === "complete") ? wpo.getWpoInfo() : setTimeout(check, 500);
  56. })()
  57. });
  58. break;
  59. // 页面取色工具 color picker
  60. case MSG_TYPE.SHOW_COLOR_PICKER:
  61. Tarp.require('../color-picker/index', true).then(picker => picker.handler(request, sender, callback));
  62. break;
  63. // 编码规范检测
  64. case MSG_TYPE.CODE_STANDARDS:
  65. Tarp.require('../code-standards/index', true).then(cs => {
  66. if (request.event === MSG_TYPE.FCP_HELPER_INIT) {
  67. cs.init();
  68. } else if (MSG_TYPE.FCP_HELPER_DETECT) {
  69. cs.detect();
  70. }
  71. });
  72. break;
  73. }
  74. });