index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. Tarp.require('../json-format/automatic').format(request.options);
  38. break;
  39. // js、css页面自动检测,提示格式化
  40. case MSG_TYPE.JS_CSS_PAGE_BEAUTIFY:
  41. Tarp.require('../code-beautify/automatic', true).then(beautifier => beautifier.detect(request.content));
  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/inject', 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. case MSG_TYPE.GRID_RULER:
  75. Tarp.require('../ruler/index',true).then(ruler => ruler.detect(callback));
  76. break;
  77. }
  78. });