|
3 | 3 | // @name:zh-CN 代码片段高亮 |
4 | 4 | // @name:zh-TW 程式碼片斷高亮 |
5 | 5 | // @namespace hoothin |
6 | | -// @version 0.81 |
| 6 | +// @version 1.0 |
7 | 7 | // @description Add a icon to popup a window that allows syntax highlighting and beautify and word count of source code snippets on current page |
8 | 8 | // @description:zh-CN 选择代码片段后点击图标弹出新窗口显示高亮美化与格式化后的代码与字数统计 |
9 | 9 | // @description:zh-TW 選擇程式碼片段後點選圖示彈出新視窗顯示高亮美化與格式化後的程式碼與字數統計 |
|
20 | 20 | 'use strict'; |
21 | 21 | var isChrome=unsafeWindow.chrome; |
22 | 22 | var codeIcon=document.createElement("img"); |
23 | | - var codes, scrollX, scrollY; |
| 23 | + var codes, selStr, scrollX, scrollY; |
24 | 24 | codeIcon.style.cssText="position:fixed;z-index:99999;display:none;cursor: pointer;"; |
25 | 25 | codeIcon.title="Show this code snippet"; |
26 | 26 | codeIcon.src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAYAgMAAACD0OXYAAAACVBMVEX7+/swMDBTU1MLxgsCAAAAJElEQVQI12MIBYEAGLUKBBbAqAUMQICgAoAqoBQ95JaCnASjAAgXMdk3d5HTAAAAAElFTkSuQmCC"; |
|
35 | 35 | 'Code formatting: <a href="#" onclick="code.innerHTML=js_beautify(codeStr.replace(/&lt;/g, \'<\').replace(/&gt;/g, \'>\'));code.className=\'prettyprint linenums\';prettyPrint();return false;">Javaspcript</a> '+ |
36 | 36 | '<a href="#" onclick="code.innerHTML=html_beautify(codeStr);code.className=\'prettyprint linenums\';prettyPrint();return false;">Html</a> '+ |
37 | 37 | '<a href="#" onclick="code.innerHTML=css_beautify(codeStr);code.className=\'prettyprint linenums\';prettyPrint();return false;">Css</a> '+ |
38 | | - '<a href="#" onclick="code.innerHTML=codeStr;code.className=\'prettyprint linenums\';prettyPrint();return false;">Raw</a> <b style="color:red">('+codes.length+' words)</b>'+ |
| 38 | + '<a href="#" onclick="code.innerHTML=codeStr;code.className=\'prettyprint linenums\';prettyPrint();return false;">Raw</a> <b style="color:red">('+selStr.length+' words)</b>'+ |
39 | 39 | '<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>"; |
40 | 40 | if(isChrome){ |
41 | 41 | let c = window.open("", "_blank", "width=750, height=400, location=0, resizable=1, menubar=0, scrollbars=0"); |
|
52 | 52 | document.addEventListener('mouseup', function(o) { |
53 | 53 | if (o.button === 0 && (o.ctrlKey || o.altKey || o.metaKey || o.shiftKey)) { |
54 | 54 | setTimeout(function(){ |
55 | | - codes = document.getSelection().toString().replace(/&/g, "&").replace(/\</g,"<").replace(/\>/g,">"); |
| 55 | + selStr=document.getSelection().toString(); |
| 56 | + codes=selStr.replace(/&/g, "&").replace(/\</g,"<").replace(/\>/g,">"); |
56 | 57 | if(codes){ |
57 | 58 | codeIcon.style.display="block"; |
58 | 59 | let pos=getMousePos(o); |
|
0 commit comments