代码片段高亮

选择代码片段后点击图标弹出新窗口显示语法高亮美化与格式化后的代码与字数统计

当前为 2021-12-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Highlight Every Code
  3. // @name:zh-CN 代码片段高亮
  4. // @name:zh-TW 程式碼片斷高亮
  5. // @namespace hoothin
  6. // @version 1.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. // @description:zh-CN 选择代码片段后点击图标弹出新窗口显示语法高亮美化与格式化后的代码与字数统计
  9. // @description:zh-TW 選擇程式碼片段後點選圖示彈出新視窗顯示語法高亮美化與格式化後的程式碼與字數統計
  10. // @author Hoothin
  11. // @grant GM_openInTab
  12. // @compatible chrome
  13. // @compatible firefox
  14. // @license MIT License
  15. // @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=rixixi@sina.com&item_name=Greasy+Fork+donation
  16. // @contributionAmount 1
  17. // @include *
  18. // ==/UserScript==
  19.  
  20. (function() {
  21. 'use strict';
  22. var isChrome=unsafeWindow.chrome;
  23. var codeIcon=document.createElement("img");
  24. var codes, selStr, scrollX, scrollY, customInput=false;
  25. 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;";
  26. codeIcon.title="Show this code snippet";
  27. codeIcon.src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAYAgMAAACD0OXYAAAACVBMVEX7+/swMDBTU1MLxgsCAAAAJElEQVQI12MIBYEAGLUKBBbAqAUMQICgAoAqoBQ95JaCnASjAAgXMdk3d5HTAAAAAElFTkSuQmCC";
  28. codeIcon.onmousedown=function(){
  29. if(customInput){
  30. selStr=prompt("Input code here","");
  31. codes=selStr.replace(/&/g, "&amp;").replace(/\</g,"&lt;").replace(/\>/g,"&gt;");
  32. }
  33. let html='<title>Code Snippet</title>'+
  34. '<link rel="stylesheet" href="https://cdn.rawgit.com/google/code-prettify/master/styles/sons-of-obsidian.css"/>'+
  35. '<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();'+
  36. 'document.querySelector("body>a:nth-child(1)").onclick=function(){'+
  37. 'code.innerHTML=js_beautify('+
  38. 'codeStr.replace(/&gt;/g, \'>\').replace(/&lt;/g, \'<\').replace(/\'(\\\\\'|[^\'])*?\'/g, function(word){'+
  39. 'return word.replace(/>/g, \'&gt;\').replace(/</g, \'&lt;\');}'+
  40. ').replace(/\"(\\\\\"|[^\"])*?\"/g, function(word){'+
  41. 'return word.replace(/>/g, \'&gt;\').replace(/</g, \'&lt;\');}'+
  42. '));code.className=\'prettyprint linenums\';prettyPrint();return false;'+
  43. '};}</script>'+
  44. '<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js?skin=sons-of-obsidian"></script>'+
  45. '<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.4/beautify.min.js"></script>'+
  46. '<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.4/beautify-html.min.js"></script>'+
  47. '<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.4/beautify-css.min.js"></script>'+
  48. 'Code formatting: <a href="#">Javaspcript</a> '+
  49. '<a href="#" onclick="code.innerHTML=html_beautify(codeStr);code.className=\'prettyprint linenums\';prettyPrint();return false;">Html</a> '+
  50. '<a href="#" onclick="code.innerHTML=css_beautify(codeStr);code.className=\'prettyprint linenums\';prettyPrint();return false;">Css</a> '+
  51. '<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>'+
  52. '<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>";
  53. if(isChrome){
  54. let c = window.open("", "_blank", "width=750, height=400, location=0, resizable=1, menubar=0, scrollbars=0");
  55. c.document.write(html);
  56. c.document.close();
  57. }else{
  58. GM_openInTab('data:text/html;charset=utf-8,' + encodeURIComponent(html));
  59. }
  60. };
  61.  
  62. document.addEventListener('mousedown', function(o) {
  63. codeIcon.style.opacity=0;
  64. customInput=false;
  65. if(codeIcon.parentNode)codeIcon.parentNode.removeChild(codeIcon);
  66. });
  67. document.addEventListener('mouseup', function(o) {
  68. if (o.button === 0 && (o.ctrlKey || o.altKey || o.metaKey || o.shiftKey)) {
  69. var customInputKey=(o.ctrlKey && o.shiftKey);
  70. setTimeout(function(){
  71. selStr=document.getSelection().toString();
  72. codes=selStr.replace(/&/g, "&amp;").replace(/\</g,"&lt;").replace(/\>/g,"&gt;");
  73. if(!codes && customInputKey){
  74. customInput=true;
  75. }
  76. if(codes || customInputKey){
  77. document.body.appendChild(codeIcon);
  78. let pos=getMousePos(o);
  79. let left, top;
  80. if(isChrome){
  81. top=pos.y>document.documentElement.clientHeight-50?(pos.y-30):(pos.y+20);
  82. left=pos.x>document.documentElement.clientWidth-50?(pos.x-30):(pos.x+20);
  83. }else{
  84. top=pos.y-30;
  85. left=pos.x+15;
  86. }
  87. codeIcon.style.opacity=0.9;
  88. codeIcon.style.top=top+"px";
  89. codeIcon.style.left=left+"px";
  90. }
  91. },1);
  92. }
  93. },false);
  94.  
  95. function getMousePos(event) {
  96. var e = event || window.event;
  97. scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
  98. scrollY = document.documentElement.scrollTop || document.body.scrollTop;
  99. var x = (e.pageX || e.clientX) - scrollX;
  100. var y = (e.pageY || e.clientY) - scrollY;
  101. return { 'x': x, 'y': y };
  102. }
  103. })();