Greasy Fork is available in English.

Mã hóa/giải mã hex và base64

8/23/2024, 8:02:26 AM

Installera detta skript?
Författaren's rekommenderade skript

Du kanske också gillar Encode/Decode Base64 or Hex with Copy Option.

Installera detta skript
  1. // ==UserScript==
  2. // @name Mã hóa/giải mã hex và base64
  3. // @namespace Violentmonkey Scripts
  4. // @match *://*/*
  5. // @grant GM_setClipboard
  6. // @version 1.0.3
  7. // @author -
  8. // @description 8/23/2024, 8:02:26 AM
  9. // @license Pirate
  10. // ==/UserScript==
  11. const Ctrl = false;
  12. const Alt = false;
  13. const Shift= false;
  14.  
  15. const isHexStr = str => /^(0x|0X)?([0-9a-fA-F]{2}\s*)+$/g.test(str.trim());
  16.  
  17. const isBase64 = str => /^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)?$/g.test(str.trim());
  18.  
  19. const refineHex = str => /(^([0-9a-fA-F]{4}\s*)+$)|(^([0-9a-fA-F]{2}\s*)+$)/.test(str.trim()) && str.replaceAll(/\s+/g, '')
  20.  
  21. function hex2Str(hexStr) {
  22. hexStr = refineHex(hexStr.toString());
  23. if (!isHexStr(hexStr)) return false;
  24. if (hexStr.startsWith('0x') || hexStr.startsWith('0X')) hexStr = hexStr.slice(2);
  25. const result = { twoDigits: '', fourDigits: '' };
  26. if (/^(00[0-9a-fA-F]{2})+$/.test(hexStr)) { //2 digit hex
  27. hexStr = hexStr.replaceAll(/00/g, '');
  28. for (let i = 0; i < hexStr.length; i += 2) result.twoDigits += String.fromCharCode(parseInt(hexStr.slice(i, i + 2), 16));
  29. return result;
  30. }
  31.  
  32. let tmpArr = hexStr.match(/[0-9a-fA-F]{2}/g);
  33. if (tmpArr.filter(el => /(0|1)[0-9a-fA-F]/.test(el)).length > 0) { //4digits
  34. for (let i = 0; i < hexStr.length; i += 4) result.fourDigits += String.fromCharCode(parseInt(hexStr.slice(i, i + 4), 16));
  35. return result;
  36. }
  37.  
  38. if (tmpArr.length % 2!=0) { //2digits
  39. for (let i = 0; i < hexStr.length; i += 2) result.twoDigits += String.fromCharCode(parseInt(hexStr.slice(i, i + 2), 16));
  40. return result;
  41. }
  42.  
  43. for (let i = 0; i < hexStr.length; i += 2) result.twoDigits += String.fromCharCode(parseInt(hexStr.slice(i, i + 2), 16));
  44. for (let i = 0; i < hexStr.length; i += 4) result.fourDigits += String.fromCharCode(parseInt(hexStr.slice(i, i + 4), 16));
  45. return result;
  46. }
  47.  
  48. function str2Hex(str) {
  49. str = str.toString().trim();
  50. let fourDigits = false;
  51. let result = '';
  52. for (let i = 0; i < str.length; i++)
  53. if (str.charCodeAt(i) < 255) result += str.charCodeAt(i).toString(16) + ' '
  54. else { fourDigits = true; break; }
  55.  
  56. if (fourDigits) {
  57. result = '';
  58. for (let i = 0; i < str.length; i++) result += ('000' + str.charCodeAt(i).toString(16)).slice(-4) + ' '
  59. }
  60. return result;
  61. }
  62.  
  63. (function main(){
  64. document.body.insertAdjacentHTML('beforeend',`
  65. <style>
  66. #us_hexdecode_dialog {
  67. max-width:60%;
  68. }
  69.  
  70. #us_hexdecode_dialog::backdrop{
  71. background-color: rgb(117 190 218 / 50%);
  72. }
  73.  
  74. #us_hexdecode_dialog>div label {
  75. width: 1rem;
  76. height: 1rem;
  77. }
  78.  
  79. #us_hexdecode_dialog>div label::before {
  80. display: inline-block;
  81. position: relative;
  82. content: "";
  83. border: 1px solid gray;
  84. width: .7rem;
  85. height: .7rem;
  86. top: .25rem;
  87. left: .01rem;
  88. background-color: lightgray;
  89. }
  90.  
  91. #us_hexdecode_dialog>div label::after {
  92. display: inline-block;
  93. position: relative;
  94. content: "";
  95. border: 1px solid gray;
  96. width: .7rem;
  97. height: .7rem;
  98. top: .15rem;
  99. left: -.85rem;
  100. background-color: lightgray;
  101. }
  102. </style>
  103. <dialog id="us_hexdecode_dialog"><div></div><button>OK</button></dialog>`);
  104. const dialog=document.getElementById('us_hexdecode_dialog');
  105. document.addEventListener("contextmenu", e => {
  106.  
  107. let str='';
  108. let aE=document.activeElement;
  109. if (aE?.tagName=='TEXTAREA' ||(aE?.tagName=='INPUT' && aE?.type=='text')) str=aE.value.substring(aE.selectionStart,aE.selectionEnd).trim();
  110. else str=document.getSelection().toString().trim();
  111.  
  112. if (str=='') return;
  113. if(e.ctrlKey!=Ctrl || e.altKey!=Alt ||e.shiftKey!=Shift) return;
  114. e.preventDefault();
  115.  
  116. const dialog_div=dialog.querySelector('div');
  117. dialog_div.innerHTML='';
  118. if (isHexStr(str)) {
  119. let hexDecode=hex2Str(str);
  120. if(hexDecode.twoDigits) dialog_div.innerHTML+='Hex 8bits decode: '+`<span>${hexDecode.twoDigits}</span> <label></label><br/><br/>`;
  121. if(hexDecode.fourDigits) dialog_div.innerHTML+='Hex 16bits decode: '+`<span>${hexDecode.fourDigits}</span> <label></label><br/><br/>`;
  122. } else dialog_div.innerHTML+=`Hex encode: <span>${str2Hex(str)}</span> <label></label><br/><br/>`;
  123.  
  124. let showedEncode=false;
  125. if (isBase64(str)) {
  126. if (!str.endsWith('=')) {
  127. dialog_div.innerHTML+=`Base64 encode: <span>${btoa(unescape(encodeURIComponent(str)))}</span> <label></label><br/><br/>`
  128. showedEncode=true;
  129. }
  130. dialog_div.innerHTML+=`Base64 decode: <span>${atob(str)}</span> <label></label>`;
  131. }
  132. else if (!showedEncode) dialog_div.innerHTML+=`Base64 encode: <span>${btoa(unescape(encodeURIComponent(str)))}</span> <label></label>`
  133.  
  134. dialog_div.querySelectorAll('label').forEach(el=>el.onclick=(e)=>GM_setClipboard(e.target.previousElementSibling.textContent));
  135. dialog.querySelector('button').onclick=()=>dialog.close();
  136. dialog.showModal();
  137. })
  138. })();