// ==UserScript== // @name HighlightEveryCode // @name:zh-CN 代码片段高亮 // @name:zh-TW 代碼片斷高亮 // @namespace hoothin // @version 0.6 // @description Add a icon that allows syntax highlighting and beautify and word count of source code snippets on current page // @description:zh-CN 选择代码片段后点击图标高亮、美化代码并显示字数统计 // @description:zh-TW 選擇代碼片段后點擊圖標高亮、美化代碼並顯示字數統計 // @author Hoothin // @include * // @downloadURL none // ==/UserScript== (function() { 'use strict'; var codeIcon=document.createElement("img"); var codes; codeIcon.style="position:fixed;z-index:99999;display:none;cursor: pointer;"; codeIcon.title="Show this code snippet"; codeIcon.src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAYAgMAAACD0OXYAAAACVBMVEX7+/swMDBTU1MLxgsCAAAAJElEQVQI12MIBYEAGLUKBBbAqAUMQICgAoAqoBQ95JaCnASjAAgXMdk3d5HTAAAAAElFTkSuQmCC"; codeIcon.onmousedown=function(){ let c = window.open("", "_blank", "width=750, height=400, location=0, resizable=1, menubar=0, scrollbars=0"); let html='
' + codes + ""; c.document.write(html); c.document.close(); }; document.body.appendChild(codeIcon); document.addEventListener('mousedown', function(o) { codeIcon.style.display="none"; }); document.addEventListener('mouseup', function(o) { if (o.button === 0 && (o.ctrlKey || o.altKey || o.metaKey)) { setTimeout(function(){ codes = document.getSelection().toString().replace(/\/g,">"); if(codes){ codeIcon.style.display="block"; let pos=getMousePos(o); codeIcon.style.top=pos.y+15+"px"; codeIcon.style.left=pos.x+15+"px"; } },1); } },false); function getMousePos(event) { var e = event || window.event; var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft; var scrollY = document.documentElement.scrollTop || document.body.scrollTop; var x = (e.pageX || e.clientX) - scrollX; var y = (e.pageY || e.clientY) - scrollY; return { 'x': x, 'y': y }; } })();