index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  29. chrome.runtime.onMessage.addListener(function (request, sender, callback) {
  30. switch (request.type) {
  31. // JSON页面自动格式化
  32. case MSG_TYPE.JSON_PAGE_FORMAT:
  33. request.canIDoIt && Tarp.require('../json-format/automatic', true).then(JsonTools => JsonTools.format());
  34. break;
  35. // 二维码解码
  36. case MSG_TYPE.QR_DECODE:
  37. Tarp.require('../qr-code/decode', true).then(qrcode => qrcode.show(request.result));
  38. break;
  39. // 页面性能检测
  40. case MSG_TYPE.GET_PAGE_WPO_INFO:
  41. Tarp.require('../wpo/inject', true).then(wpo => {
  42. (function check() {
  43. (document.readyState === "complete") ? wpo.getWpoInfo() : setTimeout(check, 500);
  44. })()
  45. });
  46. break;
  47. // 页面取色工具 color picker
  48. case MSG_TYPE.SHOW_COLOR_PICKER:
  49. Tarp.require('../color-picker/index', true).then(picker => picker.handler(request, sender, callback));
  50. break;
  51. // 编码规范检测
  52. case MSG_TYPE.CODE_STANDARDS:
  53. Tarp.require('../code-standards/index', true).then(cs => {
  54. if (request.event === MSG_TYPE.FCP_HELPER_INIT) {
  55. cs.init();
  56. } else if (MSG_TYPE.FCP_HELPER_DETECT) {
  57. cs.detect();
  58. }
  59. });
  60. break;
  61. }
  62. });