Clean Gamer.com.tw Forum Links

Cleans up gamer.com.tw redirect links

目前为 2023-08-14 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Clean Gamer.com.tw Forum Links
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Cleans up gamer.com.tw redirect links
  6. // @author CY Fung
  7. // @match https://forum.gamer.com.tw/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. const safeHosts = [
  16. 'github.com', 'forum.gamer.com.tw', 'gf.qytechs.cn', 'github.io',
  17. 'www.jsdelivr.com', 'jsdelivr.com', 'cdnjs.com',
  18. 'www.photopea.com', 'photopea.com', 'chat.openai.com', 'openai.com'
  19. ];
  20.  
  21. // TODO function for testing if a link is safe
  22. function isLinkSafe(url) {
  23. // TODO: implement the actual safety check
  24. let uo = null;
  25. try {
  26. uo = new URL(url);
  27. } catch (e) {
  28. return false;
  29. }
  30. if (uo && uo.hostname) {
  31. if (safeHosts.includes(uo.hostname)) return true;
  32. }
  33. return false;
  34. }
  35.  
  36. const clickHandler = (evt) => {
  37. evt.stopPropagation();
  38. evt.stopImmediatePropagation();
  39. };
  40.  
  41. function fixAnchor(decodedURL) {
  42. for (const anchor of document.querySelectorAll(`a[href^="${decodedURL}"]`)) {
  43. anchor.addEventListener('click', clickHandler, true);
  44. anchor.removeAttribute('onclick');
  45. anchor.onclick = null;
  46. anchor.setAttribute("data-cleaned", "true");
  47. }
  48. }
  49.  
  50. function cleanLinks() {
  51. const anchors = document.querySelectorAll('a[href^="https://ref.gamer.com.tw/redir.php?url=https"]:not([data-cleaned]), a[onclick]:not([data-cleaned])');
  52.  
  53. anchors.forEach(anchor => {
  54. anchor.setAttribute("data-cleaned", "pending");
  55. let anchorHref = anchor.getAttribute('href');
  56. let match = anchorHref.match(/url=([^&]*)/);
  57. match = match && match[1] ? decodeURIComponent(match[1]) : anchorHref;
  58. if (match && match.startsWith('https://')) {
  59. const decodedURL = match;
  60. if (isLinkSafe(decodedURL)) {
  61. anchor.setAttribute('href', decodedURL);
  62. Promise.resolve(decodedURL).then(fixAnchor);
  63. }
  64. }
  65. });
  66. }
  67.  
  68. const observer = new MutationObserver(cleanLinks);
  69. observer.observe(document.body, { childList: true, subtree: true });
  70.  
  71. // First triggering after setting up the MutationObserver
  72. cleanLinks();
  73. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址