cfemail.user.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // ==UserScript==
  2. // @name Decode Cloudflare-encoded email addresses
  3. // @namespace https://codeberg.org/smege1001/cf_email_decoder
  4. // @match *://*/*
  5. // @grant none
  6. // @version 1.2.2
  7. // @author smege1001
  8. // ==/UserScript==
  9. /**
  10. * @license CC0-1.0
  11. **/
  12. const emailprotectionURLHashRegex = /\/cdn-cgi\/l\/email-protection#([aA0-fF9]*)/;
  13. const emailprotectionURLNoHashRegex = /\/cdn-cgi\/l\/email-protection/; //hash is stored on data-cfemail
  14. function decodeEmail(hash) { //cloudflare email address decoder
  15. var hashArray = []; //split the hash into bytes
  16. for (var hAIndex = 0; hAIndex < hash.length; hAIndex += 2) {
  17. hashArray.push(parseInt(hash.substring(hAIndex, hAIndex + 2), 16));
  18. }
  19. var decoded = "";
  20. var key = hashArray[0]; //get the decode key
  21. for (var index = 1; index < hashArray.length; index++) {
  22. decoded += String.fromCharCode(hashArray[index] ^ key);
  23. }
  24. return decoded;
  25. }
  26. var links = document.querySelectorAll("a"); //get all the links
  27. for (var linksIndex = 0; linksIndex < links.length; linksIndex++) {
  28. var link = links[linksIndex];
  29. if (emailprotectionURLHashRegex.test(link.href)) {
  30. var hash = link.href.match(emailprotectionURLHashRegex)[1];
  31. var decodedEmail = decodeEmail(hash);
  32. link.href = "mailto:" + decodedEmail; //replace the stupid email protection with just a mailto link
  33. if (link.getElementsByClassName("__cf_email__").length > 0) {
  34. var linkChild = link.getElementsByClassName("__cf_email__")[0];
  35. linkChild.innerText = decodedEmail;
  36. linkChild.removeAttribute("data-cfemail");
  37. linkChild.classList.remove("__cf_email__");
  38. if (linkChild.getAttribute("class") == "") linkChild.removeAttribute("class");
  39. }
  40. } else if (emailprotectionURLNoHashRegex.test(link.href) && link.hasAttribute("data-cfemail")) {
  41. var hash = link.getAttribute("data-cfemail");
  42. var decodedEmail = decodeEmail(hash);
  43. link.href = "mailto:" + decodedEmail;
  44. link.innerText = decodedEmail; //the inner text is just [email protected]
  45. //remove the useless attributes
  46. link.removeAttribute("data-cfemail");
  47. link.classList.remove("__cf_email__");
  48. if (link.getAttribute("class") == "") link.removeAttribute("class");
  49. }
  50. }