index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 this.toast('内容为空,不需要美化处理!', 'warning');
  42. }else{
  43. this.toast('格式化进行中...', 'info');
  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('格式化完成!', 'success');
  54. });
  55. };
  56. switch (this.selectedType) {
  57. case 'Javascript':
  58. try {
  59. let opts = {
  60. brace_style: "collapse",
  61. break_chained_methods: false,
  62. indent_char: " ",
  63. indent_scripts: "keep",
  64. indent_size: "4",
  65. keep_array_indentation: true,
  66. preserve_newlines: true,
  67. space_after_anon_function: true,
  68. space_before_conditional: true,
  69. unescape_strings: false,
  70. wrap_line_length: "120",
  71. "max_preserve_newlines": "5",
  72. "jslint_happy": false,
  73. "end_with_newline": false,
  74. "indent_inner_html": false,
  75. "comma_first": false,
  76. "e4x": false
  77. };
  78. beauty(js_beautify(this.sourceContent, opts));
  79. } catch (error) {
  80. this.toast('JavaScript格式化失败,请检查代码语法!', 'error');
  81. }
  82. break;
  83. case 'CSS':
  84. try {
  85. css_beautify(this.sourceContent, {}, result => beauty(result));
  86. } catch (error) {
  87. this.toast('CSS格式化失败,请检查代码语法!', 'error');
  88. }
  89. break;
  90. case 'HTML':
  91. try {
  92. beauty(html_beautify(this.sourceContent,{indent_size:4}));
  93. } catch (error) {
  94. this.toast('HTML格式化失败,请检查代码语法!', 'error');
  95. }
  96. break;
  97. case 'SQL':
  98. try {
  99. beauty(vkbeautify.sql(this.sourceContent, 4));
  100. } catch (error) {
  101. this.toast('SQL格式化失败,请检查代码语法!', 'error');
  102. }
  103. break;
  104. default:
  105. try {
  106. beauty(vkbeautify.xml(this.sourceContent));
  107. } catch (error) {
  108. this.toast('XML格式化失败,请检查代码语法!', 'error');
  109. }
  110. }
  111. },
  112. copy: function () {
  113. let _copyToClipboard = (text) => {
  114. let input = document.createElement('textarea');
  115. input.style.position = 'fixed';
  116. input.style.opacity = 0;
  117. input.value = text;
  118. document.body.appendChild(input);
  119. input.select();
  120. document.execCommand('Copy');
  121. document.body.removeChild(input);
  122. this.toast('复制成功,随处粘贴可用!', 'success')
  123. };
  124. let txt = this.$refs.jfContentBox.textContent;
  125. _copyToClipboard(txt);
  126. },
  127. /**
  128. * 自动消失的通知弹窗,仿Notification效果
  129. * @param content 通知内容
  130. * @param type 通知类型:success、error、warning、info(默认)
  131. */
  132. toast (content, type = 'info') {
  133. window.clearTimeout(window.feHelperAlertMsgTid);
  134. let elAlertMsg = document.querySelector("#fehelper_alertmsg");
  135. // 根据类型配置样式
  136. const typeConfig = {
  137. info: {
  138. background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
  139. borderColor: '#4ade80',
  140. icon: 'ℹ'
  141. },
  142. success: {
  143. background: 'linear-gradient(135deg, #4ade80 0%, #16a34a 100%)',
  144. borderColor: '#22c55e',
  145. icon: '✓'
  146. },
  147. error: {
  148. background: 'linear-gradient(135deg, #ef4444 0%, #dc2626 100%)',
  149. borderColor: '#f87171',
  150. icon: '✕'
  151. },
  152. warning: {
  153. background: 'linear-gradient(135deg, #f59e0b 0%, #d97706 100%)',
  154. borderColor: '#fbbf24',
  155. icon: '⚠'
  156. }
  157. };
  158. const config = typeConfig[type] || typeConfig.info;
  159. if (!elAlertMsg) {
  160. let elWrapper = document.createElement('div');
  161. elWrapper.innerHTML = `
  162. <div id="fehelper_alertmsg" style="
  163. position: fixed;
  164. top: 20px;
  165. right: 20px;
  166. z-index: 10000000;
  167. min-width: 300px;
  168. max-width: 400px;
  169. opacity: 0;
  170. transform: translateX(100%);
  171. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  172. ">
  173. <div class="toast-inner" style="
  174. background: ${config.background};
  175. color: #fff;
  176. padding: 16px 20px;
  177. border-radius: 8px;
  178. box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2), 0 4px 10px rgba(0, 0, 0, 0.1);
  179. font-size: 14px;
  180. font-weight: 500;
  181. line-height: 1.4;
  182. position: relative;
  183. overflow: hidden;
  184. ">
  185. <div class="toast-border" style="
  186. position: absolute;
  187. top: 0;
  188. left: 0;
  189. width: 4px;
  190. height: 100%;
  191. background: ${config.borderColor};
  192. "></div>
  193. <div style="
  194. display: flex;
  195. align-items: center;
  196. gap: 12px;
  197. ">
  198. <div class="toast-icon" style="
  199. flex-shrink: 0;
  200. width: 20px;
  201. height: 20px;
  202. background: rgba(255, 255, 255, 0.2);
  203. border-radius: 50%;
  204. display: flex;
  205. align-items: center;
  206. justify-content: center;
  207. font-size: 12px;
  208. ">${config.icon}</div>
  209. <div class="toast-content" style="flex: 1;">${content}</div>
  210. </div>
  211. </div>
  212. </div>
  213. `;
  214. elAlertMsg = elWrapper.childNodes[1]; // 第一个是文本节点,第二个才是div
  215. document.body.appendChild(elAlertMsg);
  216. // 触发动画
  217. setTimeout(() => {
  218. elAlertMsg.style.opacity = '1';
  219. elAlertMsg.style.transform = 'translateX(0)';
  220. }, 10);
  221. } else {
  222. // 更新现有通知的内容和样式
  223. const toastInner = elAlertMsg.querySelector('.toast-inner');
  224. const toastBorder = elAlertMsg.querySelector('.toast-border');
  225. const toastIcon = elAlertMsg.querySelector('.toast-icon');
  226. const toastContent = elAlertMsg.querySelector('.toast-content');
  227. toastInner.style.background = config.background;
  228. toastBorder.style.background = config.borderColor;
  229. toastIcon.innerHTML = config.icon;
  230. toastContent.innerHTML = content;
  231. elAlertMsg.style.display = 'block';
  232. elAlertMsg.style.opacity = '1';
  233. elAlertMsg.style.transform = 'translateX(0)';
  234. }
  235. window.feHelperAlertMsgTid = window.setTimeout(function () {
  236. // 淡出动画
  237. elAlertMsg.style.opacity = '0';
  238. elAlertMsg.style.transform = 'translateX(100%)';
  239. // 动画完成后隐藏
  240. setTimeout(() => {
  241. elAlertMsg.style.display = 'none';
  242. }, 300);
  243. }, 3000);
  244. },
  245. loadExample(type,event) {
  246. if(event){
  247. event.preventDefault();
  248. }
  249. const typeMap = {
  250. 'js': 'Javascript',
  251. 'css': 'CSS',
  252. 'html': 'HTML',
  253. 'xml': 'XML',
  254. 'sql': 'SQL'
  255. };
  256. this.sourceContent = this.examples[type];
  257. this.selectedType = typeMap[type];
  258. this.toast(`已加载${typeMap[type]}示例代码`, 'info');
  259. this.$nextTick(() => {
  260. this.format();
  261. });
  262. },
  263. openOptionsPage: function(event) {
  264. event.preventDefault();
  265. event.stopPropagation();
  266. chrome.runtime.openOptionsPage();
  267. },
  268. openDonateModal: function(event ){
  269. event.preventDefault();
  270. event.stopPropagation();
  271. chrome.runtime.sendMessage({
  272. type: 'fh-dynamic-any-thing',
  273. thing: 'open-donate-modal',
  274. params: { toolName: 'code-beautify' }
  275. });
  276. }
  277. }
  278. });