automatic.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. module.exports = (() => {
  2. let formattedCodes = '';
  3. /**
  4. * 代码美化
  5. */
  6. let format = (fileType, source, callback) => {
  7. let beauty = txtResult => {
  8. formattedCodes = txtResult;
  9. txtResult = txtResult.replace(/>/g, '&gt;').replace(/</g, '&lt;');
  10. txtResult = '<pre class="language-' + fileType.toLowerCase() + ' line-numbers"><code>' + txtResult + '</code></pre>';
  11. $('#fehelper_tips').siblings().remove().end().after(txtResult);
  12. Tarp.require('../static/vendor/prism/prism.js', true).then(Prism => {
  13. Prism.highlightAll();
  14. });
  15. callback && callback();
  16. };
  17. switch (fileType) {
  18. case 'javascript':
  19. let opts = {
  20. brace_style: "collapse",
  21. break_chained_methods: false,
  22. indent_char: " ",
  23. indent_scripts: "keep",
  24. indent_size: "4",
  25. keep_array_indentation: true,
  26. preserve_newlines: true,
  27. space_after_anon_function: true,
  28. space_before_conditional: true,
  29. unescape_strings: false,
  30. wrap_line_length: "120"
  31. };
  32. Tarp.require('../code-beautify/beautify.js');
  33. js_beautify(source, opts, resp => beauty(resp));
  34. break;
  35. case 'css':
  36. Tarp.require('../code-beautify/beautify-css.js');
  37. css_beautify(source, {}, resp => beauty(resp));
  38. break;
  39. }
  40. };
  41. /**
  42. * 检测
  43. * @returns {boolean}
  44. */
  45. let detect = () => {
  46. let ext = location.pathname.substring(location.pathname.lastIndexOf(".") + 1).toLowerCase();
  47. let fileType = ({'js': 'javascript', 'css': 'css'})[ext];
  48. if (!fileType || document.contentType.toLowerCase() === 'text/html') {
  49. return false;
  50. }
  51. let source = document.body.textContent;
  52. let cssUrl = chrome.extension.getURL('code-beautify/automatic.css');
  53. $('<link href="' + cssUrl + '" rel="stylesheet" type="text/css" />').appendTo(document.head);
  54. $(document.body).addClass('show-tipsbar');
  55. let tipsBar = $('<div id="fehelper_tips">' +
  56. '<span class="desc">FeHelper检测到这可能是<i>' + fileType + '</i>代码,<span class="ask">是否进行美化处理?</span></span>' +
  57. '<button class="yes">代码美化</button>' +
  58. '<button class="no">放弃!</button>' +
  59. '<button class="copy hide">复制美化过的代码</button>' +
  60. '<button class="close"><span></span></button>' +
  61. '<a class="forbid">彻底关闭这个功能!&gt;&gt;</a>' +
  62. '</div>').prependTo('body');
  63. tipsBar.find('button.yes').click((evt) => {
  64. tipsBar.find('button.yes,button.no').hide();
  65. $('<span class="doing">正在努力,请稍后...</span>').insertBefore(tipsBar.find('button.yes'));
  66. format(fileType, source, () => {
  67. tipsBar.find('span.ask').text('已为您美化完毕!');
  68. $(document.body).removeClass('show-tipsbar').addClass('show-beautified');
  69. });
  70. });
  71. tipsBar.find('a.forbid').click((evt) => {
  72. evt.preventDefault();
  73. chrome.runtime.sendMessage({
  74. type: MSG_TYPE.OPEN_OPTIONS_PAGE
  75. });
  76. });
  77. tipsBar.find('button.no,button.close').click((evt) => {
  78. $(document.body).removeClass('show-tipsbar').removeClass('show-beautified');
  79. tipsBar.remove();
  80. });
  81. tipsBar.find('button.copy').click((evt) => {
  82. _copyToClipboard(formattedCodes);
  83. });
  84. };
  85. /**
  86. * chrome 下复制到剪贴板
  87. * @param text
  88. */
  89. let _copyToClipboard = function (text) {
  90. let input = document.createElement('textarea');
  91. input.style.position = 'fixed';
  92. input.style.opacity = 0;
  93. input.value = text;
  94. document.body.appendChild(input);
  95. input.select();
  96. document.execCommand('Copy');
  97. document.body.removeChild(input);
  98. alert('代码复制成功,随处粘贴可用!')
  99. };
  100. return {
  101. detect: detect
  102. }
  103. })();