TPT Syntax Highlighted Code Boxes

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

От 30.11.2014. Виж последната версия.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey, Greasemonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Userscripts.

За да инсталирате скрипта, трябва да инсталирате разширение като Tampermonkey.

За да инсталирате този скрипт, трябва да имате инсталиран скриптов мениджър.

(Вече имам скриптов мениджър, искам да го инсталирам!)

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

(Вече имам инсталиран мениджър на стиловете, искам да го инсталирам!)

// ==UserScript==
// @name TPT Syntax Highlighted Code Boxes
// @version 1.0.0
// @description Syntax highlights <code> boxes on the powder toy forums.
// @author boxmein
// @match *://powdertoy.co.uk/Discussions/Thread/*
// @namespace http://boxmein.net
// @id [email protected]
// ==/UserScript==
// last updated: Mon Dec 01 2014 00:26:43 GMT+0200 (FLE Standard Time)
 
// Highlight.js is the highlighting library.
var HLJS = "//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js";
 
// Language support for Lua isn't included in ^that distribution by default
var LUA = "//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/languages/lua.min.js";
 
// Highlight.js only slaps on class names, you also need to style the classes! 
var HL_STYLE = "//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/github.min.css";
 
 
(function() {
  'use strict';
  
  // Runs a function in the document. Basically like a Content Script.
  // http://wiki.greasespot.net/Content_Script_Injection
  function contentEval(source) {
 
    // Check for function input.
    if ('function' == typeof source) {
      // Execute this function with no arguments, by adding parentheses.
      // One set around the function, required for valid syntax, and a
      // second empty set calls the surrounded function.
      source = '(' + source + ')();'
    }
 
    // Create a script node holding this  source code.
    var script = document.createElement('script');
    script.setAttribute("type", "application/javascript");
    script.textContent = source;
 
    // Insert the script node into the page, so it will run, and immediately
    // remove it to clean up.
    document.body.appendChild(script);
    //document.body.removeChild(script);
  }
 
 
  // Given a src attribute, makes a <script> tag into the end of <body>
  function contentScript(source) {
    var tag = document.createElement('script');
    tag.setAttribute('type', 'application/javascript');
    tag.src = source;
 
    document.body.appendChild(tag);
    //document.body.removeChild(tag);
    return tag;
  }
 
  contentScript(HLJS);
 
  // Set an ID, because after *that* loads, hljs is finally ready to highlight
  // Lua as well as C++.
  var tag = contentScript(LUA);
  tag.id = "luaapi";
 
 
  // Add the CSS for good measure too
  var st = document.createElement('link');
  st.type = 'text/css';
  st.rel = 'stylesheet';
  st.href = HL_STYLE;
  document.head.appendChild(st);
 
  contentEval(function() {
 
    function highlightCode(){
      // by default hljs highlights <pre><code>, have to override
      var ds = document.querySelectorAll('code');
      for(var d = 0; d < ds.length; d++)
        window.hljs.highlightBlock(ds[d]);  
    }
    
    window.highlightCode = highlightCode;
 
    document.getElementById('luaapi').onload = highlightCode;
  });
 
})();