automatic.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. callback && callback();
  15. });
  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. beauty(js_beautify(source, opts));
  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 = (fileType) => {
  46. let source = document.body.textContent;
  47. let cssUrl = chrome.extension.getURL('code-beautify/automatic.css');
  48. $('<link href="' + cssUrl + '" rel="stylesheet" type="text/css" />').appendTo(document.head);
  49. $(document.body).addClass('show-tipsbar');
  50. let tipsBar = $('<div id="fehelper_tips">' +
  51. '<span class="desc">FeHelper检测到这可能是<i>' + fileType + '</i>代码,<span class="ask">是否进行美化处理?</span></span>' +
  52. '<a class="encoding">有乱码?点击修正!</a>' +
  53. '<button class="yes">代码美化</button>' +
  54. '<button class="no">放弃!</button>' +
  55. '<button class="copy hide">复制美化过的代码</button>' +
  56. '<button class="close"><span></span></button>' +
  57. '<a class="forbid">彻底关闭这个功能!&gt;&gt;</a>' +
  58. '</div>').prependTo('body');
  59. tipsBar.find('button.yes').click((evt) => {
  60. tipsBar.find('button.yes,button.no').hide();
  61. let elAsk = tipsBar.find('span.ask').text('正在努力美化,请稍候...');
  62. format(fileType, source, () => {
  63. elAsk.text('已为您美化完毕!');
  64. $(document.body).removeClass('show-tipsbar').addClass('show-beautified');
  65. });
  66. });
  67. tipsBar.find('a.forbid').click((evt) => {
  68. evt.preventDefault();
  69. chrome.runtime.sendMessage({
  70. type: MSG_TYPE.OPEN_OPTIONS_PAGE
  71. });
  72. });
  73. tipsBar.find('button.no,button.close').click((evt) => {
  74. $(document.body).removeClass('show-tipsbar').removeClass('show-beautified');
  75. tipsBar.remove();
  76. });
  77. tipsBar.find('button.copy').click((evt) => {
  78. _copyToClipboard(formattedCodes);
  79. });
  80. tipsBar.find('a.encoding').click((evt) => {
  81. evt.preventDefault();
  82. fetch(location.href).then(res => res.text()).then(text => {
  83. source = text;
  84. if ($(document.body).hasClass('show-beautified')) {
  85. tipsBar.find('button.yes').trigger('click');
  86. } else {
  87. $('#fehelper_tips+pre').text(text);
  88. }
  89. });
  90. });
  91. };
  92. /**
  93. * chrome 下复制到剪贴板
  94. * @param text
  95. */
  96. let _copyToClipboard = function (text) {
  97. let input = document.createElement('textarea');
  98. input.style.position = 'fixed';
  99. input.style.opacity = 0;
  100. input.value = text;
  101. document.body.appendChild(input);
  102. input.select();
  103. document.execCommand('Copy');
  104. document.body.removeChild(input);
  105. alert('代码复制成功,随处粘贴可用!')
  106. };
  107. return {
  108. detect: detect
  109. }
  110. })();