content-script.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. window.codebeautifyContentScript = (() => {
  2. let __importScript = (filename) => {
  3. let url = filename;
  4. if (location.protocol === 'chrome-extension:' || chrome.runtime && chrome.runtime.getURL) {
  5. url = chrome.runtime.getURL('code-beautify/' + filename);
  6. }
  7. fetch(url).then(resp => resp.text()).then(jsText => {
  8. if(window.evalCore && window.evalCore.getEvalInstance){
  9. return window.evalCore.getEvalInstance(window)(jsText);
  10. }
  11. let el = document.createElement('script');
  12. el.textContent = jsText;
  13. document.head.appendChild(el);
  14. });
  15. };
  16. __importScript('beautify.js');
  17. __importScript('beautify-css.js');
  18. let highlightWebWorker = () => {
  19. let __importScript = (filename) => {
  20. let url = filename;
  21. if (location.protocol === 'chrome-extension:' || typeof chrome !='undefined' && chrome.runtime && chrome.runtime.getURL) {
  22. url = chrome.runtime.getURL('code-beautify/' + filename);
  23. }
  24. fetch(url).then(resp => resp.text()).then(jsText => {
  25. if(window.evalCore && window.evalCore.getEvalInstance){
  26. return window.evalCore.getEvalInstance(window)(jsText);
  27. }
  28. let el = document.createElement('script');
  29. el.textContent = jsText;
  30. document.head.appendChild(el);
  31. });
  32. };
  33. let site = 'chrome-extension://mnaedlmagdcfmejjndjhffalddfofeim';
  34. __importScript(site + '/static/vendor/highlight/highlight.js');
  35. self.onmessage = (event) => {
  36. const result = self.hljs.highlightAuto(event.data);
  37. postMessage(result.value);
  38. };
  39. };
  40. let formattedCodes = '';
  41. // **************************************************************
  42. /**
  43. * 代码美化
  44. */
  45. let format = (fileType, source, callback) => {
  46. let beauty = txtResult => {
  47. let code = document.getElementsByTagName('pre')[0];
  48. formattedCodes = txtResult;
  49. code.textContent = txtResult;
  50. code.classList.add('language-' + fileType.toLowerCase());
  51. // 用webwork的方式来进行格式化,效率更高
  52. let worker = new Worker(URL.createObjectURL(new Blob(["(" + highlightWebWorker.toString() + ")()"], {type: 'text/javascript'})));
  53. worker.onmessage = (event) => {
  54. code.innerHTML = "<ol><li><span>" + event.data.replace(/\n/gm, '</span></li><li><span>') + '</span></li></ol>';
  55. callback && callback();
  56. };
  57. worker.postMessage(txtResult);
  58. };
  59. switch (fileType) {
  60. case 'javascript':
  61. let opts = {
  62. brace_style: "collapse",
  63. break_chained_methods: false,
  64. indent_char: " ",
  65. indent_scripts: "keep",
  66. indent_size: "4",
  67. keep_array_indentation: true,
  68. preserve_newlines: true,
  69. space_after_anon_function: true,
  70. space_before_conditional: true,
  71. unescape_strings: false,
  72. wrap_line_length: "120"
  73. };
  74. beauty(js_beautify(source, opts));
  75. break;
  76. case 'css':
  77. css_beautify(source, {}, resp => beauty(resp));
  78. break;
  79. }
  80. };
  81. /**
  82. * 检测
  83. * @returns {boolean}
  84. */
  85. window._codebutifydetect_ = (fileType) => {
  86. if (!document.getElementsByTagName('pre')[0]) {
  87. return;
  88. }
  89. let source = document.getElementsByTagName('pre')[0].textContent;
  90. $(document.body).addClass('show-tipsbar');
  91. let tipsBar = $('<div id="fehelper_tips">' +
  92. '<span class="desc">FeHelper检测到这可能是<i>' + fileType + '</i>代码,<span class="ask">是否进行美化处理?</span></span>' +
  93. '<a class="encoding">有乱码?点击修正!</a>' +
  94. '<button class="yes">代码美化</button>' +
  95. '<button class="no">放弃!</button>' +
  96. '<button class="copy hide">复制美化过的代码</button>' +
  97. '<button class="close"><span></span></button>' +
  98. '<a class="forbid">彻底关闭这个功能!&gt;&gt;</a>' +
  99. '</div>').prependTo('body');
  100. tipsBar.find('button.yes').click((evt) => {
  101. tipsBar.find('button.yes,button.no').hide();
  102. let elAsk = tipsBar.find('span.ask').text('正在努力美化,请稍候...');
  103. format(fileType, source, () => {
  104. elAsk.text('已为您美化完毕!');
  105. $(document.body).removeClass('show-tipsbar').addClass('show-beautified');
  106. });
  107. });
  108. tipsBar.find('a.forbid').click((evt) => {
  109. evt.preventDefault();
  110. if (confirm('一旦彻底关闭,不可恢复,请确认?')) {
  111. chrome.runtime.sendMessage({
  112. type: 'fh-dynamic-any-thing',
  113. thing: 'close-beautify'
  114. }, () => {
  115. alert('已关闭,如果要恢复,请在FeHelper「设置页」重新安装「代码美化工具」!');
  116. });
  117. }
  118. });
  119. tipsBar.find('button.no,button.close').click((evt) => {
  120. $(document.body).removeClass('show-tipsbar').removeClass('show-beautified');
  121. tipsBar.remove();
  122. });
  123. tipsBar.find('button.copy').click((evt) => {
  124. _copyToClipboard(formattedCodes);
  125. });
  126. tipsBar.find('a.encoding').click((evt) => {
  127. evt.preventDefault();
  128. fetch(location.href).then(res => res.text()).then(text => {
  129. source = text;
  130. if ($(document.body).hasClass('show-beautified')) {
  131. tipsBar.find('button.yes').trigger('click');
  132. } else {
  133. $('#fehelper_tips+pre').text(text);
  134. }
  135. });
  136. });
  137. };
  138. /**
  139. * chrome 下复制到剪贴板
  140. * @param text
  141. */
  142. let _copyToClipboard = function (text) {
  143. let input = document.createElement('textarea');
  144. input.style.position = 'fixed';
  145. input.style.opacity = 0;
  146. input.value = text;
  147. document.body.appendChild(input);
  148. input.select();
  149. document.execCommand('Copy');
  150. document.body.removeChild(input);
  151. alert('代码复制成功,随处粘贴可用!')
  152. };
  153. return function () {
  154. let ext = location.pathname.substring(location.pathname.lastIndexOf(".") + 1).toLowerCase();
  155. let fileType = ({'js': 'javascript', 'css': 'css'})[ext];
  156. let contentType = document.contentType.toLowerCase();
  157. if (!fileType) {
  158. if (/\/javascript$/.test(contentType)) {
  159. fileType = 'javascript';
  160. } else if (/\/css$/.test(contentType)) {
  161. fileType = 'css';
  162. }
  163. } else if (contentType === 'text/html') {
  164. fileType = undefined;
  165. }
  166. chrome.runtime.sendMessage({
  167. type: 'fh-dynamic-any-thing',
  168. thing: 'code-beautify',
  169. params: { fileType, tabId: window.__FH_TAB_ID__ || null }
  170. });
  171. };
  172. })();