TPT Syntax Highlighted Code Boxes

Syntax highlights <code> boxes on the powder toy forums.

目前为 2014-11-30 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name TPT Syntax Highlighted Code Boxes
  3. // @version 1.0.0
  4. // @description Syntax highlights <code> boxes on the powder toy forums.
  5. // @author boxmein
  6. // @match *://powdertoy.co.uk/Discussions/Thread/*
  7. // @namespace http://boxmein.net
  8. // @id 53666432360934db4cd6@boxmein.net
  9. // ==/UserScript==
  10. // last updated: Mon Dec 01 2014 00:26:43 GMT+0200 (FLE Standard Time)
  11. // Highlight.js is the highlighting library.
  12. var HLJS = "//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js";
  13. // Language support for Lua isn't included in ^that distribution by default
  14. var LUA = "//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/languages/lua.min.js";
  15. // Highlight.js only slaps on class names, you also need to style the classes!
  16. var HL_STYLE = "//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/github.min.css";
  17. (function() {
  18. 'use strict';
  19. // Runs a function in the document. Basically like a Content Script.
  20. // http://wiki.greasespot.net/Content_Script_Injection
  21. function contentEval(source) {
  22. // Check for function input.
  23. if ('function' == typeof source) {
  24. // Execute this function with no arguments, by adding parentheses.
  25. // One set around the function, required for valid syntax, and a
  26. // second empty set calls the surrounded function.
  27. source = '(' + source + ')();'
  28. }
  29. // Create a script node holding this source code.
  30. var script = document.createElement('script');
  31. script.setAttribute("type", "application/javascript");
  32. script.textContent = source;
  33. // Insert the script node into the page, so it will run, and immediately
  34. // remove it to clean up.
  35. document.body.appendChild(script);
  36. //document.body.removeChild(script);
  37. }
  38. // Given a src attribute, makes a <script> tag into the end of <body>
  39. function contentScript(source) {
  40. var tag = document.createElement('script');
  41. tag.setAttribute('type', 'application/javascript');
  42. tag.src = source;
  43. document.body.appendChild(tag);
  44. //document.body.removeChild(tag);
  45. return tag;
  46. }
  47. contentScript(HLJS);
  48. // Set an ID, because after *that* loads, hljs is finally ready to highlight
  49. // Lua as well as C++.
  50. var tag = contentScript(LUA);
  51. tag.id = "luaapi";
  52. // Add the CSS for good measure too
  53. var st = document.createElement('link');
  54. st.type = 'text/css';
  55. st.rel = 'stylesheet';
  56. st.href = HL_STYLE;
  57. document.head.appendChild(st);
  58. contentEval(function() {
  59. function highlightCode(){
  60. // by default hljs highlights <pre><code>, have to override
  61. var ds = document.querySelectorAll('code');
  62. for(var d = 0; d < ds.length; d++)
  63. window.hljs.highlightBlock(ds[d]);
  64. }
  65. window.highlightCode = highlightCode;
  66. document.getElementById('luaapi').onload = highlightCode;
  67. });
  68. })();