Syntaxify

Universal syntax highlighting

当前为 2015-06-25 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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;
		}
	}
});