Show Character Codes

Display a dialog showing the character code(s) of the selected character(s). This script is instended to be used as a bookmarklet using this URL: javascript:scc_ujs()

当前为 2018-02-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Show Character Codes
  3. // @namespace https://greasyfork.org/en/users/85671-jcunews
  4. // @version 1.0.2
  5. // @license AGPL v3
  6. // @description Display a dialog showing the character code(s) of the selected character(s). This script is instended to be used as a bookmarklet using this URL: javascript:scc_ujs()
  7. // @author jcunews
  8. // @match *://*/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. window.scc_ujs = function(maxTextLength, wspaces, txt, ei, a, b, i, c, o, d, e) {
  13.  
  14. //===== CONFIG START =====
  15.  
  16. maxTextLength = 100;
  17.  
  18. //===== CONFIG END =====
  19.  
  20. function zerolead(s, l) {
  21. return ("0").repeat(l - s.length) + s;
  22. }
  23.  
  24. function hex(n, h) {
  25. h = n.toString(16).toUpperCase();
  26. if (n <= 0xff) {
  27. return zerolead(h, 2);
  28. } else if (n <= 0xffff) {
  29. return zerolead(h, 4);
  30. } else return zerolead(h, 8);
  31. }
  32.  
  33. txt = getSelection();
  34. if ((txt.anchorNode === txt.focusNode) && (txt.anchorOffset === txt.focusOffset)) {
  35. if (!(txt = document.activeElement) || !("value" in txt)) return;
  36. txt = txt.value.substring(txt.selectionStart, txt.selectionEnd);
  37. } else txt = txt.toString();
  38. if (!txt || document.querySelector('div.showCharCode[id^="scc"]')) return;
  39. ei = "scc" + (new Date()).getTime();
  40. a = document.createElement("DIV");
  41. a.id = ei;
  42. a.className = "showCharCode"
  43. a.innerHTML = `
  44. <style>
  45. #${ei} * {
  46. opacity:1!important; visibility:visible!important;
  47. position:static!important; z-index:auto!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; float:none!important;
  48. margin:0!important; vertical-align:baseline!important;
  49. border:none!important; padding:0!important; width:auto!important; height:auto!important; overflow:visible!important;
  50. background:transparent!important; text-align:left!important; text-decoration:none!important; color:#000!important;
  51. font:normal normal normal 16pt/normal sans-serif!important; cursor:auto!important;
  52. }
  53. #${ei}, #${ei} .curtain {
  54. display:block!important; position:fixed!important; z-index:999999996!important; left:0!important; top:0!important; right:0!important; bottom:0!important;
  55. }
  56. #${ei} .curtain {
  57. opacity:.3!important; z-index:999999997!important; background:#000!important; cursor:pointer!important;
  58. }
  59. #${ei} .dialog {
  60. display:block!important; z-index:999999998!important; visibility:visible!important;
  61. position:absolute!important;
  62. padding:10px 10px 0 10px!important; border:5px solid #55b!important; border-radius:10px!important;
  63. max-height:90%!important; overflow-y:scroll!important; background:#fff!important;
  64. }
  65. #${ei} label {
  66. margin-right:2ex!important; cursor:pointer!important;
  67. }
  68. #${ei} label:last-child {
  69. margin-right:0!important;
  70. }
  71. #${ei} table {
  72. margin:10px 0!important;
  73. }
  74. #${ei} th {
  75. padding:0 1ex!important; background:#000!important; text-align:center!important; color:#fff!important;
  76. }
  77. #${ei} table.no1Bytes .bytes1 {
  78. display:none!important;
  79. }
  80. #${ei} table.no2Bytes .bytes2 {
  81. display:none!important;
  82. }
  83. #${ei} table.no4Bytes .bytes4 {
  84. display:none!important;
  85. }
  86. #${ei} table.noWSpaces .wspace {
  87. display:none!important;
  88. }
  89. #${ei} td {
  90. padding:0 1ex!important; background:#ddd!important;
  91. }
  92. #${ei} td:first-child {
  93. position:relative!important; text-align:center!important;
  94. }
  95. #${ei} td:first-child:hover:after {
  96. position:absolute!important; z-index:999999999!important; top:-.54em!important;
  97. border: 2px solid #007; border-radius:10px!important; padding:0 .11em!important; background:#fff!important;
  98. font-size:80px; content:attr(char);
  99. }
  100. #${ei} tr:nth-child(2)~tr td:first-child:hover:after {
  101. top:-.89em!important;
  102. }
  103. #${ei} td+td {
  104. text-align:right!important;
  105. }
  106. </style>
  107. <div class="curtain"></div>
  108. <div class="dialog">
  109. <div style="white-space:nowrap!important">
  110. <label for="${ei}1bytes"><input id="${ei}1bytes" type="checkbox" checked /> 1-Byte</label>
  111. <label for="${ei}2bytes"><input id="${ei}2bytes" type="checkbox" checked /> 2-Bytes</label>
  112. <label for="${ei}4bytes"><input id="${ei}4bytes" type="checkbox" checked /> 4-Bytes</label>
  113. <label for="${ei}wspaces"><input id="${ei}wspaces" type="checkbox" /> White-Spaces</label>
  114. </div>
  115. <table class="noWSpaces">
  116. <tr><th>Char</th><th>Hex</th><th>Dec</th><th>UTF-16 Codes</th><th>UTF-8 Codes</th></tr>
  117. </table>
  118. </div>
  119. `;
  120. a.children[1].addEventListener("click", function() {
  121. a.remove();
  122. document.documentElement.style.overflow = "";
  123. }, true);
  124. b = a.lastElementChild.lastElementChild;
  125. a.querySelector("#" + ei + "1bytes").addEventListener("click", function() {
  126. b.classList.toggle("no1Bytes");
  127. }, true);
  128. a.querySelector("#" + ei + "2bytes").addEventListener("click", function() {
  129. b.classList.toggle("no2Bytes");
  130. }, true);
  131. a.querySelector("#" + ei + "4bytes").addEventListener("click", function() {
  132. b.classList.toggle("no4Bytes");
  133. }, true);
  134. a.querySelector("#" + ei + "wspaces").addEventListener("click", function() {
  135. b.classList.toggle("noWSpaces");
  136. }, true);
  137. wspaces = "\t\n\r \u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u202f\u205f\u3000\ufeff";
  138. i = 0;
  139. while (i < txt.length) {
  140. d = txt.codePointAt(i);
  141. (e = (o = b.insertRow()).insertCell()).textContent = c = String.fromCodePoint(d);
  142. e.setAttribute("char", c);
  143. if (c.length === 1) {
  144. o.className = ((c.charCodeAt(0) > 0xff) ? "bytes2" : "bytes1") + (wspaces.indexOf(c) >= 0 ? " wspace" : "");
  145. } else o.className = "bytes4";
  146. o.insertCell().textContent = "0x" + hex(d);
  147. o.insertCell().textContent = d;
  148. o.insertCell().textContent = c.split("").reduce(
  149. function(p, c) {
  150. p.push(hex(c.charCodeAt(0)));
  151. return p;
  152. }, []
  153. ).join(",");
  154. o.insertCell().textContent = unescape(encodeURIComponent(c)).split("").map(
  155. function(w) {
  156. return hex(w.charCodeAt(0));
  157. }
  158. ).join(",");
  159. i += c.length;
  160. }
  161. document.body.appendChild(a);
  162. c = b.parentNode;
  163. c.style.cssText =
  164. "left:" + Math.floor((innerWidth - c.offsetWidth) / 2) + "px!important;" +
  165. "top:" + Math.floor((innerHeight - c.offsetHeight) / 2) + "px!important";
  166. document.documentElement.style.overflow = "hidden";
  167. };