automatic.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. module.exports = (() => {
  2. /**
  3. * 代码美化
  4. */
  5. let format = (fileType, source, callback) => {
  6. let beauty = txtResult => {
  7. txtResult = txtResult.replace(/>/g, '&gt;').replace(/</g, '&lt;');
  8. txtResult = '<pre class="brush: ' + fileType.toLowerCase() + ';toolbar:false;">' + txtResult + '</pre>';
  9. document.body.innerHTML = txtResult;
  10. // 代码高亮
  11. let map = {
  12. core: '../static/vendor/syntaxhighlighter/shCore.js',
  13. Javascript: '../static/vendor/syntaxhighlighter/shBrushJScript.js',
  14. CSS: '../static/vendor/syntaxhighlighter/shBrushCss.js'
  15. };
  16. Tarp.require(map.core, true).then(SH => {
  17. Tarp.require(map[fileType], true).then(SH => {
  18. SH.defaults['toolbar'] = false;
  19. SH.highlight();
  20. });
  21. });
  22. callback && callback();
  23. };
  24. switch (fileType) {
  25. case 'Javascript':
  26. let opts = {
  27. brace_style: "collapse",
  28. break_chained_methods: false,
  29. indent_char: " ",
  30. indent_scripts: "keep",
  31. indent_size: "4",
  32. keep_array_indentation: true,
  33. preserve_newlines: true,
  34. space_after_anon_function: true,
  35. space_before_conditional: true,
  36. unescape_strings: false,
  37. wrap_line_length: "120"
  38. };
  39. Tarp.require('../code-beautify/beautify.js');
  40. js_beautify(source, opts, resp => beauty(resp));
  41. break;
  42. case 'CSS':
  43. Tarp.require('../code-beautify/beautify-css.js');
  44. css_beautify(source, {}, resp => beauty(resp));
  45. break;
  46. }
  47. };
  48. /**
  49. * 检测
  50. * @returns {boolean}
  51. */
  52. let detect = () => {
  53. let ext = location.pathname.substring(location.pathname.lastIndexOf(".") + 1).toLowerCase();
  54. let fileType = ({'js': 'Javascript', 'css': 'CSS'})[ext];
  55. if (!fileType || document.contentType.toLowerCase() === 'text/html') {
  56. return false;
  57. }
  58. let source = document.body.textContent;
  59. let cssUrl = chrome.extension.getURL('code-beautify/automatic.css');
  60. $('<link href="' + cssUrl + '" rel="stylesheet" type="text/css" />').appendTo(document.head);
  61. $(document.body).addClass('show-tipsbar');
  62. let tipsBar = $('<div id="fehelper_tips">' +
  63. '<span class="desc">FeHelper检测到这可能是<i>' + fileType + '</i>代码,是否进行美化处理?</span>' +
  64. '<button class="yes">代码美化</button>' +
  65. '<button class="no">放弃!</button>' +
  66. '<button class="close"><span></span></button>' +
  67. '<a class="forbid">彻底关闭这个功能!&gt;&gt;</a>' +
  68. '</div>').prependTo('body');
  69. tipsBar.find('button.yes').click((evt) => {
  70. tipsBar.find('button.yes,button.no').hide();
  71. $('<span class="doing">正在努力,请稍后...</span>').insertBefore(tipsBar.find('button.yes'));
  72. format(fileType, source,()=>{
  73. $(document.body).removeClass('show-tipsbar').addClass('show-beautified');
  74. });
  75. });
  76. tipsBar.find('a.forbid').click((evt) => {
  77. evt.preventDefault();
  78. chrome.runtime.sendMessage({
  79. type: MSG_TYPE.OPEN_OPTIONS_PAGE
  80. });
  81. });
  82. tipsBar.find('button.no,button.close').click((evt) => {
  83. $(document.body).removeClass('show-tipsbar');
  84. });
  85. };
  86. return {
  87. detect: detect
  88. }
  89. })();