程式碼片斷高亮

選擇程式碼片段後點選圖示彈出新視窗顯示語法高亮美化與格式化後的程式碼與字數統計

目前為 2022-01-07 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Highlight Every Code
  3. // @name:zh-CN 代码片段高亮
  4. // @name:zh-TW 程式碼片斷高亮
  5. // @namespace hoothin
  6. // @version 2.0
  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. // @description:zh-CN 选择代码片段后点击图标弹出新窗口显示语法高亮美化与格式化后的代码与字数统计
  9. // @description:zh-TW 選擇程式碼片段後點選圖示彈出新視窗顯示語法高亮美化與格式化後的程式碼與字數統計
  10. // @author Hoothin
  11. // @grant GM_openInTab
  12. // @grant unsafeWindow
  13. // @compatible chrome
  14. // @compatible firefox
  15. // @license MIT License
  16. // @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=rixixi@sina.com&item_name=Greasy+Fork+donation
  17. // @contributionAmount 1
  18. // @include *
  19. // ==/UserScript==
  20.  
  21. (function() {
  22. 'use strict';
  23. var codeIcon=document.createElement("img");
  24. var codes, selStr, scrollX, scrollY, customInput=false;
  25. var _unsafeWindow=(typeof unsafeWindow=='undefined'? window : unsafeWindow);
  26. 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;";
  27. codeIcon.title="Show this code snippet";
  28. 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, "&amp;").replace(/\</g,"&lt;").replace(/\>/g,"&gt;");
  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(/&amp;/g, "&").replace(/&(nbsp;|amp;|#39;|quot;)/g, "&amp;$1");prettyPrint();'+
  38. 'document.querySelector("body>a:nth-child(1)").onclick=function(){'+
  39. 'code.innerHTML=js_beautify('+
  40. 'codeStr.replace(/&gt;/g, \'>\').replace(/&lt;/g, \'<\').replace(/\'(\\\\\'|[^\'])*?\'/g, function(word){'+
  41. 'return word.replace(/>/g, \'&gt;\').replace(/</g, \'&lt;\');}'+
  42. ').replace(/\"(\\\\\"|[^\"])*?\"/g, function(word){'+
  43. 'return word.replace(/>/g, \'&gt;\').replace(/</g, \'&lt;\');}'+
  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. };
  60.  
  61. document.addEventListener('DOMMouseScroll', function(o) {
  62. hideIcon();
  63. });
  64. document.addEventListener('wheel', function(o) {
  65. hideIcon();
  66. });
  67. document.addEventListener('mousewheel', function(o) {
  68. hideIcon();
  69. });
  70. document.addEventListener('mouseover', function(o) {
  71. if(o.target.nodeName!="PRE" && o.target.nodeName!="CODE")return;
  72. selStr=o.target.innerText;
  73. if(!selStr)return;
  74. codes=selStr.replace(/&/g, "&amp;").replace(/\</g,"&lt;").replace(/\>/g,"&gt;");
  75. document.body.appendChild(codeIcon);
  76. let pos=getMousePos(o);
  77. scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
  78. scrollY = document.documentElement.scrollTop || document.body.scrollTop;
  79. let top=o.target.offsetTop-scrollY;
  80. let left=o.target.offsetLeft-scrollX;
  81. codeIcon.style.opacity=0.9;
  82. codeIcon.style.top=top+"px";
  83. codeIcon.style.left=left+"px";
  84. });
  85. document.addEventListener('mousedown', function(o) {
  86. hideIcon();
  87. });
  88. document.addEventListener('mouseup', function(o) {
  89. if (o.button === 0 && (o.ctrlKey || o.altKey || o.metaKey || o.shiftKey)) {
  90. //var customInputKey=(o.ctrlKey && o.shiftKey);
  91. setTimeout(function(){
  92. selStr=document.getSelection().toString();
  93. codes=selStr.replace(/&/g, "&amp;").replace(/\</g,"&lt;").replace(/\>/g,"&gt;");
  94. if(!codes){
  95. customInput=true;
  96. }
  97. //if(codes || customInputKey){
  98. document.body.appendChild(codeIcon);
  99. let pos=getMousePos(o);
  100. let clientH=(document.documentElement && document.documentElement.clientHeight) || 0;
  101. let clientW=(document.documentElement && document.documentElement.clientWidth) || 0;
  102. let top=pos.y>clientH-50?(pos.y-30):(pos.y+20);
  103. let left=pos.x>clientW-50?(pos.x-30):(pos.x+20);
  104. codeIcon.style.opacity=0.9;
  105. codeIcon.style.top=top+"px";
  106. codeIcon.style.left=left+"px";
  107. //}
  108. },1);
  109. }
  110. },false);
  111.  
  112. function hideIcon(){
  113. codeIcon.style.opacity=0;
  114. customInput=false;
  115. if(codeIcon.parentNode)codeIcon.parentNode.removeChild(codeIcon);
  116. }
  117. function getMousePos(event) {
  118. var e = event || window.event;
  119. scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
  120. scrollY = document.documentElement.scrollTop || document.body.scrollTop;
  121. var x = (e.pageX || e.clientX) - scrollX;
  122. var y = (e.pageY || e.clientY) - scrollY;
  123. return { 'x': x, 'y': y };
  124. }
  125. })();