33// @name :zh-CN 代码片段高亮
44// @name :zh-TW 程式碼片斷高亮
55// @namespace hoothin
6- // @version 2.0
6+ // @version 2.0.1
77// @description Add a icon to popup a window that allows syntax highlighting and beautify and word count of source code snippets on current page
88// @description :zh-CN 选择代码片段后点击图标弹出新窗口显示语法高亮美化与格式化后的代码与字数统计
99// @description :zh-TW 選擇程式碼片段後點選圖示彈出新視窗顯示語法高亮美化與格式化後的程式碼與字數統計
1010// @author Hoothin
11- // @grant GM_openInTab
11+ // @grant GM_registerMenuCommand
12+ // @grant GM.registerMenuCommand
1213// @grant unsafeWindow
1314// @compatible chrome
1415// @compatible firefox
2627 codeIcon . style . cssText = "position:fixed;z-index:99999;cursor:pointer;transition:opacity 0.5s ease-in-out 0s;opacity:0;border:5px solid rgba(0, 0, 0, 0.2);border-radius:10px;" ;
2728 codeIcon . title = "Show this code snippet" ;
2829 codeIcon . src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAYAgMAAACD0OXYAAAACVBMVEX7+/swMDBTU1MLxgsCAAAAJElEQVQI12MIBYEAGLUKBBbAqAUMQICgAoAqoBQ95JaCnASjAAgXMdk3d5HTAAAAAElFTkSuQmCC" ;
29- codeIcon . onmousedown = function ( ) {
30- if ( customInput ) {
31- selStr = prompt ( "Input code here" , "" ) ;
32- if ( ! selStr ) return ;
33- codes = selStr . replace ( / & / g, "&" ) . replace ( / \< / g, "<" ) . replace ( / \> / g, ">" ) ;
34- }
35- let html = '<title>Code Snippet</title>' +
36- '<link rel="stylesheet" href="https://cdn.rawgit.com/google/code-prettify/master/styles/sons-of-obsidian.css"/>' +
37- '<script>var code,codeStr;window.onload=function(){code=document.querySelector("#code");codeStr=code.innerHTML.replace(/&/g, "&").replace(/&(nbsp;|amp;|#39;|quot;)/g, "&$1");prettyPrint();' +
38- 'document.querySelector("body>a:nth-child(1)").onclick=function(){' +
39- 'code.innerHTML=js_beautify(' +
40- 'codeStr.replace(/>/g, \'>\').replace(/</g, \'<\').replace(/\'(\\\\\'|[^\'])*?\'/g, function(word){' +
41- 'return word.replace(/>/g, \'>\').replace(/</g, \'<\');}' +
42- ').replace(/\"(\\\\\"|[^\"])*?\"/g, function(word){' +
43- 'return word.replace(/>/g, \'>\').replace(/</g, \'<\');}' +
44- '));code.className=\'prettyprint linenums\';prettyPrint();return false;' +
45- '};}</script>' +
46- '<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js?skin=sons-of-obsidian"></script>' +
47- '<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.4/beautify.min.js"></script>' +
48- '<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.4/beautify-html.min.js"></script>' +
49- '<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.4/beautify-css.min.js"></script>' +
50- 'Code formatting: <a href="#">Javaspcript</a> ' +
51- '<a href="#" onclick="code.innerHTML=html_beautify(codeStr);code.className=\'prettyprint linenums\';prettyPrint();return false;">Html</a> ' +
52- '<a href="#" onclick="code.innerHTML=css_beautify(codeStr);code.className=\'prettyprint linenums\';prettyPrint();return false;">Css</a> ' +
53- '<a href="#" onclick="code.innerHTML=codeStr;code.className=\'prettyprint linenums\';prettyPrint();return false;">Raw</a> <b style="color:red">(' + selStr . replace ( / \s / g, "" ) . length + ' words)</b>' +
54- '<pre id="code" class="prettyprint linenums" style="word-wrap: break-word; white-space: pre-wrap;border: 1px solid rgb(136, 136, 204);border-radius: 8px;">' + codes + "</pre>" ;
55-
56- let c = _unsafeWindow . open ( "" , "_blank" , "width=750, height=400, location=0, resizable=1, menubar=0, scrollbars=0" ) ;
57- c . document . write ( html ) ;
58- c . document . close ( ) ;
59- } ;
30+ codeIcon . onmousedown = highlight ;
6031
6132 document . addEventListener ( 'DOMMouseScroll' , function ( o ) {
6233 hideIcon ( ) ;
9364 selStr = document . getSelection ( ) . toString ( ) ;
9465 codes = selStr . replace ( / & / g, "&" ) . replace ( / \< / g, "<" ) . replace ( / \> / g, ">" ) ;
9566 if ( ! codes ) {
96- customInput = true ;
67+ return ;
9768 }
98- //if(codes || customInputKey){
9969 document . body . appendChild ( codeIcon ) ;
10070 let pos = getMousePos ( o ) ;
10171 let clientH = ( document . documentElement && document . documentElement . clientHeight ) || 0 ;
10575 codeIcon . style . opacity = 0.9 ;
10676 codeIcon . style . top = top + "px" ;
10777 codeIcon . style . left = left + "px" ;
108- //}
10978 } , 1 ) ;
11079 }
11180 } , false ) ;
11281
82+ function highlight ( ) {
83+ if ( customInput ) {
84+ selStr = prompt ( "Input code here" , "" ) ;
85+ if ( ! selStr ) return ;
86+ codes = selStr . replace ( / & / g, "&" ) . replace ( / \< / g, "<" ) . replace ( / \> / g, ">" ) ;
87+ }
88+ let html = '<title>Code Snippet</title>' +
89+ '<link rel="stylesheet" href="https://cdn.rawgit.com/google/code-prettify/master/styles/sons-of-obsidian.css"/>' +
90+ '<script>var code,codeStr;window.onload=function(){code=document.querySelector("#code");codeStr=code.innerHTML.replace(/&/g, "&").replace(/&(nbsp;|amp;|#39;|quot;)/g, "&$1");prettyPrint();' +
91+ 'document.querySelector("body>a:nth-child(1)").onclick=function(){' +
92+ 'code.innerHTML=js_beautify(' +
93+ 'codeStr.replace(/>/g, \'>\').replace(/</g, \'<\').replace(/\'(\\\\\'|[^\'])*?\'/g, function(word){' +
94+ 'return word.replace(/>/g, \'>\').replace(/</g, \'<\');}' +
95+ ').replace(/\"(\\\\\"|[^\"])*?\"/g, function(word){' +
96+ 'return word.replace(/>/g, \'>\').replace(/</g, \'<\');}' +
97+ '));code.className=\'prettyprint linenums\';prettyPrint();return false;' +
98+ '};}</script>' +
99+ '<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js?skin=sons-of-obsidian"></script>' +
100+ '<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.4/beautify.min.js"></script>' +
101+ '<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.4/beautify-html.min.js"></script>' +
102+ '<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.4/beautify-css.min.js"></script>' +
103+ 'Code formatting: <a href="#">Javaspcript</a> ' +
104+ '<a href="#" onclick="code.innerHTML=html_beautify(codeStr);code.className=\'prettyprint linenums\';prettyPrint();return false;">Html</a> ' +
105+ '<a href="#" onclick="code.innerHTML=css_beautify(codeStr);code.className=\'prettyprint linenums\';prettyPrint();return false;">Css</a> ' +
106+ '<a href="#" onclick="code.innerHTML=codeStr;code.className=\'prettyprint linenums\';prettyPrint();return false;">Raw</a> <b style="color:red">(' + selStr . replace ( / \s / g, "" ) . length + ' words)</b>' +
107+ '<pre id="code" class="prettyprint linenums" style="word-wrap: break-word; white-space: pre-wrap;border: 1px solid rgb(136, 136, 204);border-radius: 8px;">' + codes + "</pre>" ;
108+
109+ let c = _unsafeWindow . open ( "" , "_blank" , "width=750, height=400, location=0, resizable=1, menubar=0, scrollbars=0" ) ;
110+ c . document . write ( html ) ;
111+ c . document . close ( ) ;
112+ }
113113 function hideIcon ( ) {
114114 codeIcon . style . opacity = 0 ;
115115 customInput = false ;
123123 var y = ( e . pageY || e . clientY ) - scrollY ;
124124 return { 'x' : x , 'y' : y } ;
125125 }
126+ var _GM_registerMenuCommand = GM_registerMenuCommand || ( typeof GM != 'undefined' && GM . registerMenuCommand ) ;
127+ if ( ! _GM_registerMenuCommand ) _GM_registerMenuCommand = ( s , f ) => { } ;
128+ _GM_registerMenuCommand ( "Highlight" , ( ) => {
129+ selStr = document . getSelection ( ) . toString ( ) ;
130+ codes = selStr . replace ( / & / g, "&" ) . replace ( / \< / g, "<" ) . replace ( / \> / g, ">" ) ;
131+ if ( ! codes ) {
132+ customInput = true ;
133+ }
134+ highlight ( ) ;
135+ } ) ;
126136} ) ( ) ;
0 commit comments