index.js 2.9 KB

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