Decode Hex strings on Voz

Decode Hex, Base64

当前为 2024-08-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Decode Hex strings on Voz
  3. // @namespace Decode Hex strings on Voz
  4. // @version 2.0
  5. // @icon https://www.google.com/s2/favicons?sz=64&domain=voz.vn
  6. // @author kylyte
  7. // @description Decode Hex, Base64
  8. // @match https://voz.vn/t/*
  9. // @run-at document-idle
  10. // @license GPL-3.0
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. function decodeHex(hexString) {
  17. if (!/^[0-9A-Fa-f]+$/.test(hexString)) return hexString;
  18. let hexStr = '';
  19. try {
  20. for (let i = 0; i < hexString.length; i += 2) {
  21. hexStr += String.fromCharCode(parseInt(hexString.substr(i, 2), 16));
  22. }
  23. return /^[\w\s.,!?@#$%^&*()_+=[\]{}|;:'",<>\-\/`~]*$/.test(hexStr) ? hexStr : hexString;
  24. } catch {
  25. return hexString;
  26. }
  27. }
  28.  
  29. function decodeBase64(base64String) {
  30. if (!/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/.test(base64String)) return base64String;
  31. try {
  32. let binaryString = atob(base64String);
  33. return /^[\w\s.,!?@#$%^&*()_+=[\]{}|;:'",<>\-\/`~]*$/.test(binaryString) ? binaryString : base64String;
  34. } catch {
  35. return base64String;
  36. }
  37. }
  38.  
  39. function decodeContent(elements, regex, decodeFunc) {
  40. for (let element of elements) {
  41. if (element.classList.contains('decoded')) continue;
  42. let content = element.innerHTML;
  43. let matches = content.match(regex);
  44. if (matches) {
  45. matches.forEach(match => {
  46. content = content.replace(new RegExp(escapeRegExp(match), 'g'), decodeFunc(match));
  47. });
  48. element.innerHTML = content;
  49. element.classList.add('decoded');
  50. }
  51. }
  52. }
  53.  
  54. function escapeRegExp(string) {
  55. return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  56. }
  57.  
  58. function main() {
  59. let elements = document.getElementsByClassName('bbCodeBlock-content');
  60. decodeContent(elements, /([0-9A-Fa-f]{2}){8,}/g, decodeHex);
  61. decodeContent(elements, /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$/m, decodeBase64);
  62. }
  63.  
  64. main();
  65.  
  66. new MutationObserver(() => main()).observe(document.documentElement, {
  67. childList: true,
  68. subtree: true
  69. });
  70. })();