index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * FeHelper 代码美化工具
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. selectedType: 'Javascript',
  8. sourceContent: '',
  9. resultContent: '',
  10. showCopyBtn: false
  11. },
  12. mounted: function () {
  13. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  14. if (location.protocol === 'chrome-extension:') {
  15. chrome.tabs.query({currentWindow: true,active: true, }, (tabs) => {
  16. let activeTab = tabs.filter(tab => tab.active)[0];
  17. chrome.runtime.sendMessage({
  18. type: 'fh-dynamic-any-thing',
  19. thing: 'request-page-content',
  20. tabId: activeTab.id
  21. }).then(resp => {
  22. if(!resp || !resp.content) return ;
  23. this.sourceContent = resp.content;
  24. this.format();
  25. });
  26. });
  27. }
  28. //输入框聚焦
  29. this.$refs.codeSource.focus();
  30. },
  31. methods: {
  32. format: function () {
  33. if (!this.sourceContent.trim()) {
  34. return alert('内容为空,不需要美化处理!');
  35. }else{
  36. this.toast('格式化进行中...');
  37. }
  38. let beauty = (result) => {
  39. result = result.replace(/>/g, '&gt;').replace(/</g, '&lt;');
  40. result = '<pre class="language-' + this.selectedType.toLowerCase() + ' line-numbers"><code>' + result + '</code></pre>';
  41. this.resultContent = result;
  42. // 代码高亮
  43. this.$nextTick(() => {
  44. Prism.highlightAll();
  45. this.showCopyBtn = true;
  46. this.toast('格式化完成!');
  47. });
  48. };
  49. switch (this.selectedType) {
  50. case 'Javascript':
  51. let opts = {
  52. brace_style: "collapse",
  53. break_chained_methods: false,
  54. indent_char: " ",
  55. indent_scripts: "keep",
  56. indent_size: "4",
  57. keep_array_indentation: true,
  58. preserve_newlines: true,
  59. space_after_anon_function: true,
  60. space_before_conditional: true,
  61. unescape_strings: false,
  62. wrap_line_length: "120",
  63. "max_preserve_newlines": "5",
  64. "jslint_happy": false,
  65. "end_with_newline": false,
  66. "indent_inner_html": false,
  67. "comma_first": false,
  68. "e4x": false
  69. };
  70. beauty(js_beautify(this.sourceContent, opts));
  71. break;
  72. case 'CSS':
  73. css_beautify(this.sourceContent, {}, result => beauty(result));
  74. break;
  75. case 'HTML':
  76. beauty(html_beautify(this.sourceContent,{indent_size:15}));
  77. break;
  78. case 'SQL':
  79. beauty(vkbeautify.sql(this.sourceContent, 4));
  80. break;
  81. default:
  82. beauty(vkbeautify.xml(this.sourceContent));
  83. }
  84. },
  85. copy: function () {
  86. let _copyToClipboard = function (text) {
  87. let input = document.createElement('textarea');
  88. input.style.position = 'fixed';
  89. input.style.opacity = 0;
  90. input.value = text;
  91. document.body.appendChild(input);
  92. input.select();
  93. document.execCommand('Copy');
  94. document.body.removeChild(input);
  95. alert('复制成功,随处粘贴可用!')
  96. };
  97. let txt = this.$refs.jfContentBox.textContent;
  98. _copyToClipboard(txt);
  99. },
  100. /**
  101. * 自动消失的Alert弹窗
  102. * @param content
  103. */
  104. toast (content) {
  105. window.clearTimeout(window.feHelperAlertMsgTid);
  106. let elAlertMsg = document.querySelector("#fehelper_alertmsg");
  107. if (!elAlertMsg) {
  108. let elWrapper = document.createElement('div');
  109. elWrapper.innerHTML = '<div id="fehelper_alertmsg" style="position:fixed;bottom:5px;left:5px;z-index:1000000">' +
  110. '<p style="background:#000;display:inline-block;color:#fff;text-align:center;' +
  111. 'padding:10px 10px;margin:0 auto;font-size:14px;border-radius:4px;">' + content + '</p></div>';
  112. elAlertMsg = elWrapper.childNodes[0];
  113. document.body.appendChild(elAlertMsg);
  114. } else {
  115. elAlertMsg.querySelector('p').innerHTML = content;
  116. elAlertMsg.style.display = 'block';
  117. }
  118. window.feHelperAlertMsgTid = window.setTimeout(function () {
  119. elAlertMsg.style.display = 'none';
  120. }, 3000);
  121. }
  122. }
  123. });