index.js 4.9 KB

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