Javascript-css beautify

Beautify and syntax highlighting for source code javascript, json, css. Support to view the source code by holding the right mouse and drag.

目前为 2018-05-16 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Javascript-css beautify
  3. // @namespace http://devs.forumvi.com
  4. // @description Beautify and syntax highlighting for source code javascript, json, css. Support to view the source code by holding the right mouse and drag.
  5. // @version 2.5.0
  6. // @icon http://i.imgur.com/kz8nqz1.png
  7. // @author Zzbaivong
  8. // @oujs:author baivong
  9. // @license MIT; https://baivong.mit-license.org/license.txt
  10. // @match http://*/*
  11. // @match https://*/*
  12. // @resource light https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css
  13. // @resource dark https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow-night.min.css
  14. // @require https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.7.5/beautify.min.js
  15. // @require https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.7.5/beautify-css.min.js
  16. // @require https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js
  17. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?v=a834d46
  18. // @noframes
  19. // @supportURL https://github.com/lelinhtinh/Userscript/issues
  20. // @run-at document-idle
  21. // @grant GM.getResourceUrl
  22. // ==/UserScript==
  23.  
  24. /* global css_beautify, js_beautify, hljs */
  25. (function () {
  26.  
  27. 'use strict';
  28.  
  29. var theme = 'dark', // light|dark
  30. lineColor = {
  31. light: ['#a7a7a7', '#e8e8e7'],
  32. dark: ['#4d4d4d', '#3a3a3a']
  33. },
  34. bgColor = {
  35. light: '#ffffff',
  36. dark: '#1d1f21'
  37. },
  38.  
  39. url = location.pathname,
  40. doc = document,
  41. contenttype = doc.contentType;
  42.  
  43. function scrollByDragging(container, disableH, disableV) {
  44.  
  45. function mouseUp(e) {
  46. if (e.which !== 3) return;
  47.  
  48. window.removeEventListener('mousemove', mouseMove, true);
  49. container.style.cursor = 'default';
  50. }
  51.  
  52. function mouseDown(e) {
  53. if (e.which !== 3) return;
  54.  
  55. pos = {
  56. x: e.clientX,
  57. y: e.clientY
  58. };
  59.  
  60. window.addEventListener('mousemove', mouseMove, true);
  61. container.style.cursor = 'move';
  62. }
  63.  
  64. function mouseMove(e) {
  65. if (!disableH) container.scrollLeft -= (-pos.x + (pos.x = e.clientX));
  66. if (!disableV) container.scrollTop -= (-pos.y + (pos.y = e.clientY));
  67. }
  68.  
  69. var pos = {
  70. x: 0,
  71. y: 0
  72. };
  73.  
  74. container.oncontextmenu = function (e) {
  75. e.preventDefault();
  76. };
  77.  
  78. container.addEventListener('mousedown', mouseDown, false);
  79. window.addEventListener('mouseup', mouseUp, false);
  80.  
  81. }
  82.  
  83. if (/^application\/(x-javascript|javascript|json)|text\/css$/.test(contenttype) || (/.+\.(js|json|css)$/.test(url) && !/^application\/(xhtml+xml|xml|rss+xml)|text\/(html|xml)$/.test(contenttype))) {
  84.  
  85. var output = doc.getElementsByTagName('pre')[0],
  86. txt = output.textContent,
  87. lang = 'javascript',
  88. lines = 0,
  89. l = '';
  90.  
  91. GM_getResourceText(theme).then(function (res) {
  92. GM_addStyle(res + 'html,body,pre{margin:0;padding:0;background:' + bgColor[theme] + '}.hljs{word-wrap:normal!important;white-space:pre!important;padding-left:4em;line-height:100%}.hljs::before{content:attr(data-lines);position:absolute;color:' + lineColor[theme][0] + ';text-align:right;width:3.5em;left:-.5em;border-right:1px solid ' + lineColor[theme][1] + ';padding-right:.5em}');
  93. });
  94.  
  95. if (contenttype === 'text/css' || /.+\.css$/.test(url)) {
  96. lang = 'css';
  97. txt = css_beautify(txt);
  98. } else {
  99. txt = js_beautify(txt);
  100. }
  101.  
  102. output.textContent = txt;
  103. output.setAttribute('class', lang);
  104.  
  105. hljs.highlightBlock(output);
  106.  
  107. lines = txt.split('\n');
  108. lines = lines ? lines.length : 0;
  109. for (var i = 0; i < lines; i++) {
  110. l += (i + 1) + '\n';
  111. }
  112.  
  113. output.setAttribute('data-lines', l);
  114. output.style.width = output.scrollWidth + 'px';
  115.  
  116. scrollByDragging(doc.body);
  117. scrollByDragging(doc.documentElement);
  118.  
  119. }
  120.  
  121. }());