ESJ Zone: Unify Domain Names

Unify internal links to use the current mirror.

  1. // ==UserScript==
  2. // @name ESJ Zone: Unify Domain Names
  3. // @name:zh-TW ESJ Zone:統一域名
  4. // @name:zh-CN ESJ Zone:统一网域
  5. // @description Unify internal links to use the current mirror.
  6. // @description:zh-TW 統一內部連結使用目前鏡像站點。
  7. // @description:zh-CN 统一内部链接使用目前镜像站点。
  8. // @icon https://icons.duckduckgo.com/ip3/www.esjzone.cc.ico
  9. // @author Jason Kwok
  10. // @namespace https://jasonhk.dev/
  11. // @version 1.0.1
  12. // @license MIT
  13. // @match https://www.esjzone.cc/*
  14. // @match https://www.esjzone.me/*
  15. // @run-at document-end
  16. // @grant none
  17. // @supportURL https://greasyfork.org/scripts/487304/feedback
  18. // ==/UserScript==
  19.  
  20. (async function ()
  21. {
  22. const WHITELISTED_HOSTNAMES = ["www.esjzone.cc", "www.esjzone.me"];
  23.  
  24. function handleAnchor(anchor)
  25. {
  26. if (!anchor.href) { return false; }
  27.  
  28. const url = new URL(anchor.href);
  29. if (!WHITELISTED_HOSTNAMES.includes(url.hostname)) { return false; }
  30.  
  31. if (url.hostname !== location.hostname)
  32. {
  33. url.hostname = location.hostname;
  34. anchor.href = url.href;
  35.  
  36. return true;
  37. }
  38.  
  39. return false;
  40. }
  41.  
  42. async function handleAnchorAsync(anchor)
  43. {
  44. return handleAnchor(anchor);
  45. }
  46.  
  47. const results = await Promise.all(Array.from(document.getElementsByTagName("a")).map(handleAnchorAsync));
  48. console.debug(`Updated ${results.reduce((last, result) => (last + result), 0)} URL(s).`);
  49. })();