index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * FeHelper 代码美化工具
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. selectedType: 'Javascript',
  8. sourceContent: '',
  9. resultContent: '',
  10. showCopyBtn: false,
  11. examples: {
  12. js: `function foo(){var x=10;if(x>5){return x*2;}else{return x/2;}}`,
  13. css: `.header{position:fixed;top:0;left:0;width:100%;background:#fff;z-index:100;}.header .logo{float:left;margin:10px;}.header .nav{float:right;}`,
  14. html: `<div class="container"><div class="header"><h1>标题</h1><nav><ul><li><a href="#">首页</a></li><li><a href="#">关于</a></li></ul></nav></div><div class="content"><p>内容区域</p></div></div>`,
  15. xml: `<?xml version="1.0" encoding="UTF-8"?><root><person><name>张三</name><age>25</age><city>北京</city></person><person><name>李四</name><age>30</age><city>上海</city></person></root>`,
  16. sql: `SELECT u.name,o.order_id,p.product_name FROM users u LEFT JOIN orders o ON u.id=o.user_id LEFT JOIN products p ON o.product_id=p.id WHERE u.status='active' AND o.create_time>='2024-01-01' ORDER BY o.create_time DESC;`
  17. }
  18. },
  19. mounted: function () {
  20. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  21. if (location.protocol === 'chrome-extension:') {
  22. chrome.tabs.query({currentWindow: true,active: true, }, (tabs) => {
  23. let activeTab = tabs.filter(tab => tab.active)[0];
  24. chrome.runtime.sendMessage({
  25. type: 'fh-dynamic-any-thing',
  26. thing: 'request-page-content',
  27. tabId: activeTab.id
  28. }).then(resp => {
  29. if(!resp || !resp.content) return ;
  30. this.sourceContent = resp.content;
  31. this.format();
  32. });
  33. });
  34. }
  35. //输入框聚焦
  36. this.$refs.codeSource.focus();
  37. },
  38. methods: {
  39. format: function () {
  40. if (!this.sourceContent.trim()) {
  41. return alert('内容为空,不需要美化处理!');
  42. }else{
  43. this.toast('格式化进行中...');
  44. }
  45. let beauty = (result) => {
  46. result = result.replace(/>/g, '&gt;').replace(/</g, '&lt;');
  47. result = '<pre class="language-' + this.selectedType.toLowerCase() + ' line-numbers"><code>' + result + '</code></pre>';
  48. this.resultContent = result;
  49. // 代码高亮
  50. this.$nextTick(() => {
  51. Prism.highlightAll();
  52. this.showCopyBtn = true;
  53. this.toast('格式化完成!');
  54. });
  55. };
  56. switch (this.selectedType) {
  57. case 'Javascript':
  58. let opts = {
  59. brace_style: "collapse",
  60. break_chained_methods: false,
  61. indent_char: " ",
  62. indent_scripts: "keep",
  63. indent_size: "4",
  64. keep_array_indentation: true,
  65. preserve_newlines: true,
  66. space_after_anon_function: true,
  67. space_before_conditional: true,
  68. unescape_strings: false,
  69. wrap_line_length: "120",
  70. "max_preserve_newlines": "5",
  71. "jslint_happy": false,
  72. "end_with_newline": false,
  73. "indent_inner_html": false,
  74. "comma_first": false,
  75. "e4x": false
  76. };
  77. beauty(js_beautify(this.sourceContent, opts));
  78. break;
  79. case 'CSS':
  80. css_beautify(this.sourceContent, {}, result => beauty(result));
  81. break;
  82. case 'HTML':
  83. beauty(html_beautify(this.sourceContent,{indent_size:4}));
  84. break;
  85. case 'SQL':
  86. beauty(vkbeautify.sql(this.sourceContent, 4));
  87. break;
  88. default:
  89. beauty(vkbeautify.xml(this.sourceContent));
  90. }
  91. },
  92. copy: function () {
  93. let _copyToClipboard = function (text) {
  94. let input = document.createElement('textarea');
  95. input.style.position = 'fixed';
  96. input.style.opacity = 0;
  97. input.value = text;
  98. document.body.appendChild(input);
  99. input.select();
  100. document.execCommand('Copy');
  101. document.body.removeChild(input);
  102. alert('复制成功,随处粘贴可用!')
  103. };
  104. let txt = this.$refs.jfContentBox.textContent;
  105. _copyToClipboard(txt);
  106. },
  107. /**
  108. * 自动消失的Alert弹窗
  109. * @param content
  110. */
  111. toast (content) {
  112. window.clearTimeout(window.feHelperAlertMsgTid);
  113. let elAlertMsg = document.querySelector("#fehelper_alertmsg");
  114. if (!elAlertMsg) {
  115. let elWrapper = document.createElement('div');
  116. elWrapper.innerHTML = '<div id="fehelper_alertmsg" style="position:fixed;bottom:5px;left:5px;z-index:1000000">' +
  117. '<p style="background:#000;display:inline-block;color:#fff;text-align:center;' +
  118. 'padding:10px 10px;margin:0 auto;font-size:14px;border-radius:4px;">' + content + '</p></div>';
  119. elAlertMsg = elWrapper.childNodes[0];
  120. document.body.appendChild(elAlertMsg);
  121. } else {
  122. elAlertMsg.querySelector('p').innerHTML = content;
  123. elAlertMsg.style.display = 'block';
  124. }
  125. window.feHelperAlertMsgTid = window.setTimeout(function () {
  126. elAlertMsg.style.display = 'none';
  127. }, 3000);
  128. },
  129. loadExample(type,event) {
  130. if(event){
  131. event.preventDefault();
  132. }
  133. const typeMap = {
  134. 'js': 'Javascript',
  135. 'css': 'CSS',
  136. 'html': 'HTML',
  137. 'xml': 'XML',
  138. 'sql': 'SQL'
  139. };
  140. this.sourceContent = this.examples[type];
  141. this.selectedType = typeMap[type];
  142. this.$nextTick(() => {
  143. this.format();
  144. });
  145. },
  146. openOptionsPage: function(event) {
  147. event.preventDefault();
  148. event.stopPropagation();
  149. chrome.runtime.openOptionsPage();
  150. },
  151. openDonateModal: function(event ){
  152. event.preventDefault();
  153. event.stopPropagation();
  154. chrome.runtime.sendMessage({
  155. type: 'fh-dynamic-any-thing',
  156. thing: 'open-donate-modal',
  157. params: { toolName: 'code-beautify' }
  158. });
  159. }
  160. }
  161. });