Syntaxify

Universal syntax highlighting

目前为 2015-06-24 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Syntaxify
  3. // @description Universal syntax highlighting
  4. // @namespace http://rob-bolton.co.uk
  5. // @version 1.0
  6. // @include http*
  7. // @grant GM_addStyle
  8. // @require http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/highlight.min.js
  9. // ==/UserScript==
  10. //
  11.  
  12. // HightlightJS CSS
  13. var cssStyle=".hljs{display:block;overflow-x:auto;padding:0.5em;background:#f0f0f0;-webkit-text-size-adjust:none}.hljs,.hljs-subst,.hljs-tag .hljs-title,.nginx .hljs-title{color:black}.hljs-string,.hljs-title,.hljs-constant,.hljs-parent,.hljs-tag .hljs-value,.hljs-rule .hljs-value,.hljs-preprocessor,.hljs-pragma,.hljs-name,.haml .hljs-symbol,.ruby .hljs-symbol,.ruby .hljs-symbol .hljs-string,.hljs-template_tag,.django .hljs-variable,.smalltalk .hljs-class,.hljs-addition,.hljs-flow,.hljs-stream,.bash .hljs-variable,.pf .hljs-variable,.apache .hljs-tag,.apache .hljs-cbracket,.tex .hljs-command,.tex .hljs-special,.erlang_repl .hljs-function_or_atom,.asciidoc .hljs-header,.markdown .hljs-header,.coffeescript .hljs-attribute,.tp .hljs-variable{color:#800}.smartquote,.hljs-comment,.hljs-annotation,.diff .hljs-header,.hljs-chunk,.asciidoc .hljs-blockquote,.markdown .hljs-blockquote{color:#888}.hljs-number,.hljs-date,.hljs-regexp,.hljs-literal,.hljs-hexcolor,.smalltalk .hljs-symbol,.smalltalk .hljs-char,.go .hljs-constant,.hljs-change,.lasso .hljs-variable,.makefile .hljs-variable,.asciidoc .hljs-bullet,.markdown .hljs-bullet,.asciidoc .hljs-link_url,.markdown .hljs-link_url{color:#080}.hljs-label,.ruby .hljs-string,.hljs-decorator,.hljs-filter .hljs-argument,.hljs-localvars,.hljs-array,.hljs-attr_selector,.hljs-important,.hljs-pseudo,.hljs-pi,.haml .hljs-bullet,.hljs-doctype,.hljs-deletion,.hljs-envvar,.hljs-shebang,.apache .hljs-sqbracket,.nginx .hljs-built_in,.tex .hljs-formula,.erlang_repl .hljs-reserved,.hljs-prompt,.asciidoc .hljs-link_label,.markdown .hljs-link_label,.vhdl .hljs-attribute,.clojure .hljs-attribute,.asciidoc .hljs-attribute,.lasso .hljs-attribute,.coffeescript .hljs-property,.hljs-phony{color:#88f}.hljs-keyword,.hljs-id,.hljs-title,.hljs-built_in,.css .hljs-tag,.hljs-doctag,.smalltalk .hljs-class,.hljs-winutils,.bash .hljs-variable,.pf .hljs-variable,.apache .hljs-tag,.hljs-type,.hljs-typename,.tex .hljs-command,.asciidoc .hljs-strong,.markdown .hljs-strong,.hljs-request,.hljs-status,.tp .hljs-data,.tp .hljs-io{font-weight:bold}.asciidoc .hljs-emphasis,.markdown .hljs-emphasis,.tp .hljs-units{font-style:italic}.nginx .hljs-built_in{font-weight:normal}.coffeescript .javascript,.javascript .xml,.lasso .markup,.tex .hljs-formula,.xml .javascript,.xml .vbscript,.xml .css,.xml .hljs-cdata{opacity:0.5}";
  14.  
  15. GM_addStyle(cssStyle);
  16.  
  17. var menuCounter = 0;
  18.  
  19. function SyntaxifyMenu(node) {
  20. var modified = false;
  21. var originalNode;
  22. var currentNode = node;
  23. var menu = document.createElement("menu");
  24. menu.setAttribute("type", "context");
  25. menu.setAttribute("id", "SyntaxifyMenu" + menuCounter++);
  26. var menuItem = document.createElement("menuitem");
  27. menuItem.setAttribute("label", "Syntaxify - Highlight");
  28. var toggleHighlight = function() {
  29. if(modified) {
  30. var parent = currentNode.parentNode;
  31. parent.insertBefore(originalNode, currentNode);
  32. parent.removeChild(currentNode);
  33. currentNode = originalNode;
  34. menuItem.setAttribute("label", "Syntaxify - Highlight");
  35. modified = false;
  36. } else {
  37. originalNode = currentNode.cloneNode(true);
  38. hljs.highlightBlock(currentNode);
  39. menuItem.setAttribute("label", "Syntaxify - UnHighlight");
  40. modified = true;
  41. }
  42. }
  43. menuItem.addEventListener("click", toggleHighlight, false);
  44. menu.appendChild(menuItem);
  45. return menu;
  46. }
  47.  
  48. var syntaxableElements = [];
  49. var tagsToSearch = ["pre", "code"];
  50. for(var i=0; i<tagsToSearch.length; i++) {
  51. var tagsFound = document.getElementsByTagName(tagsToSearch[i]);
  52. for(var j=0; j<tagsFound.length; j++) {
  53. syntaxableElements.push(tagsFound.item(j));
  54. }
  55. }
  56.  
  57. if(syntaxableElements) {
  58. for(var i=0; i<(syntaxableElements.length || 0); i++) {
  59. var element = syntaxableElements[i];
  60. var parent = element.parentNode;
  61. var menu = new SyntaxifyMenu(element);
  62. parent.insertBefore(menu, element);
  63. element.setAttribute("contextmenu", menu.getAttribute("id"));
  64. }
  65. }