Greasy Fork is available in English.

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.

Od 15.05.2018.. Pogledajte najnovija verzija.

  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.4.3
  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. // @noframes
  18. // @supportURL https://github.com/lelinhtinh/Userscript/issues
  19. // @run-at document-idle
  20. // @grant GM_addStyle
  21. // @grant GM_getResourceText
  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_addStyle(GM_getResourceText(theme) + '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}');
  92.  
  93. if (contenttype === 'text/css' || /.+\.css$/.test(url)) {
  94. lang = 'css';
  95. txt = css_beautify(txt);
  96. } else {
  97. txt = js_beautify(txt);
  98. }
  99.  
  100. output.textContent = txt;
  101. output.setAttribute('class', lang);
  102.  
  103. hljs.highlightBlock(output);
  104.  
  105. lines = txt.split('\n');
  106. lines = lines ? lines.length : 0;
  107. for (var i = 0; i < lines; i++) {
  108. l += (i + 1) + '\n';
  109. }
  110.  
  111. output.setAttribute('data-lines', l);
  112. output.style.width = output.scrollWidth + 'px';
  113.  
  114. scrollByDragging(doc.body);
  115. scrollByDragging(doc.documentElement);
  116.  
  117. }
  118.  
  119. }());