Syntaxify

Universal syntax highlighting

As of 25. 06. 2015. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name Syntaxify
// @description Universal syntax highlighting
// @namespace http://rob-bolton.co.uk
// @version 1.3
// @include http*
// @grant GM_addStyle
// @grant GM_getResourceText
// @require http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/highlight.min.js
// @resource highlightJsCss https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/styles/monokai.min.css
// ==/UserScript==
//

var tagsToSearch = ["pre", "code"];

GM_addStyle(GM_getResourceText("highlightJsCss"));

var codeBlocks = {};
var itemCounter = 0;
var selectedItem;

function isCodeBlock(node) {
	if(node) {
		var tagname = node.tagName.toLowerCase();
		for(var i=0; i<tagsToSearch.length; i++) {
			if(tagname == tagsToSearch[i]) {
				return true;
			}
		}
	}
	
	return false;
}

function wrapSelection() {
	var selection = window.getSelection();
	if(selection.rangeCount && tagsToSearch.indexOf(selection.anchorNode.nodeName) == -1) {
		range = selection.getRangeAt(0);
		if(range) {
			range.surroundContents(new CodeContainer());
		}
	}
}

function CodeContainer() {
	var container = document.createElement("code");
	var menu = addSyntaxMenuForNode(container);
	
	var menuItem = document.createElement("menuitem");
	menuItem.setAttribute("label", "Unwrap code");
	menuItem.addEventListener("click", function(){
		var parent = container.parentNode;
		var newTextNode = document.createTextNode(container.textContent);
		parent.insertBefore(newTextNode, container);
		parent.removeChild(container);
	});
	
	menu.appendChild(menuItem);
	
	return container;
}

function addBlock(node) {
	if(!node.id) {
		node.id = "SyntaxifyBlock" + itemCounter++;
	}
	codeBlocks[node.id] = {
		"node": node,
		"highlighted": false,
		"originalNode": node.cloneNode()
	}
}

function revert(nodeData) {
	var parent = nodeData.node.parentNode;
	parent.insertBefore(nodeData.originalNode, nodeData.node);
	parent.removeChild(nodeData.node);
	nodeData.node = nodeData.originalNode;
	nodeData.highlighted = false;
	nodeData.node.setAttribute("contextMenu", "SyntaxifyPreMenu");
}

function highlight() {
	var node = selectedItem;
	if(node) {
		if(!codeBlocks[node.id]) {
			addBlock(node);
		}
		var nodeData = codeBlocks[node.id];
		if(nodeData.highlighted === false) {
			nodeData.originalNode = node.cloneNode(true);
			try {
				if(nodeData.lang) {
					node.className = (node.className || "") + " lang-" + nodeData.lang;
				}
				hljs.highlightBlock(node);
			} catch(e) {
				console.err(e);
				revert(nodeData);
				alert(e);
				return;
			}
			nodeData.highlighted = true;
			node.setAttribute("contextMenu", "SyntaxifyPostMenu");
		}
	}
}

function highlightAs() {
	var node = selectedItem;
	if(node) {
		if(!codeBlocks[node.id]) {
				addBlock(node);
		}
		var lang = prompt("Language:");
		if(lang) {
			codeBlocks[node.id].lang = lang;
			highlight();
		} else {
			codeBlocks[node.id].lang = null;
		}
	}
}

function unHighlight() {
	var node = selectedItem;
	if(node) {
		if(!codeBlocks[node.id]) {
			addBlock(node);
		}
		var nodeData = codeBlocks[node.id];
		if(nodeData.highlighted === true) {
			revert(nodeData);
		}
	}
}

var syntaxableElements = [];
for(var i=0; i<tagsToSearch.length; i++) {
	var tagsFound = document.getElementsByTagName(tagsToSearch[i]);
	for(var j=0; j<tagsFound.length; j++) {
		syntaxableElements.push(tagsFound.item(j));
	}
}

if(syntaxableElements) {
	for(var i=0; i<(syntaxableElements.length || 0); i++) {
		syntaxableElements[i].setAttribute("contextmenu", "SyntaxifyPreMenu");
	}
}

var body = document.body;
var bodyMenu;
var bodyMenuId = body.getAttribute("contextmenu");
if(!bodyMenuId) {
	bodyMenu = document.createElement("menu");
	bodyMenu.setAttribute("type", "context");
	bodyMenu.setAttribute("id", "SyntaxifyMenuMain");
} else {
	bodyMenu = document.getElementById(bodyMenuId);
}

var wrapItem = document.createElement("menuitem");
wrapItem.setAttribute("label", "Syntaxify - Wrap with <code>");
wrapItem.addEventListener("click", wrapSelection, false);

bodyMenu.appendChild(wrapItem);
body.appendChild(bodyMenu);
body.setAttribute("contextmenu", bodyMenu.getAttribute("id"));


var syntaxPreMenu = document.createElement("menu");
syntaxPreMenu.setAttribute("type", "context");
syntaxPreMenu.setAttribute("id", "SyntaxifyPreMenu");

var syntaxPreMenuHighlightItem = document.createElement("menuitem");
syntaxPreMenuHighlightItem.setAttribute("label", "Syntaxify - Highlight");
syntaxPreMenuHighlightItem.addEventListener("click", highlight, false);
syntaxPreMenu.appendChild(syntaxPreMenuHighlightItem);

var syntaxPreMenuHighlightAsItem = document.createElement("menuitem");
syntaxPreMenuHighlightAsItem.setAttribute("label", "Syntaxify - Highlight as...");
syntaxPreMenuHighlightAsItem.addEventListener("click", highlightAs, false);
syntaxPreMenu.appendChild(syntaxPreMenuHighlightAsItem);

body.appendChild(syntaxPreMenu);


var syntaxPostMenu = document.createElement("menu");
syntaxPostMenu.setAttribute("type", "context");
syntaxPostMenu.setAttribute("id", "SyntaxifyPostMenu");

var syntaxPostMenuUnHighlightItem = document.createElement("menuitem");
syntaxPostMenuUnHighlightItem.setAttribute("label", "Syntaxify - UnHighlight");
syntaxPostMenuUnHighlightItem.addEventListener("click", unHighlight, false);
syntaxPostMenu.appendChild(syntaxPostMenuUnHighlightItem);

body.appendChild(syntaxPostMenu);

document.addEventListener("mousedown", function(event) {
	if(event.button == 2) {
		var node = event.target;
		while(node.parentNode && !isCodeBlock(node)) {
			node = node.parentNode;
		}
		if(isCodeBlock(node)) {
			selectedItem = node;
		} else {
			selectedItem = null;
		}
	}
});