External Link Auto Redirect

redirect to the real URL directly when clicking on a link that contains a redirect URL

当前为 2024-03-01 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name External Link Auto Redirect
  3. // @name:zh-CN 外链自动重定向
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.3
  6. // @description redirect to the real URL directly when clicking on a link that contains a redirect URL
  7. // @description:zh-CN 点击包含重定向 URL 的链接时,直接重定向到真实的 URL
  8. // @author uiliugang
  9. // @run-at document-start
  10. // @match *://*/*
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. const redirectRegex = /[?&#](target|to|ac=2&url|url|remoteUrl|redirect|u|goto|link)=([^&]+)/i;
  18.  
  19. document.addEventListener('click', function(e) {
  20. // 检查点击的元素是否具有 href 属性
  21. if (e.target && e.target.href) {
  22. const url = e.target.href;
  23. const processedUrl = processUrl(url);
  24. if (processedUrl) {
  25. e.preventDefault();
  26. window.open(processedUrl, '_blank');
  27. }
  28. }
  29.  
  30. // 如果点击的元素没有 href 属性,则检查其父元素
  31. else if (e.target.parentElement && e.target.parentElement.href) {
  32. const url = e.target.parentElement.href;
  33. const processedUrl = processUrl(url);
  34. if (processedUrl) {
  35. e.preventDefault();
  36. window.open(processedUrl, '_blank');
  37. }
  38. }
  39. // 如果点击的元素或其父元素都没有 href 属性,则检查其祖先元素
  40. else if (e.target.parentElement && e.target.parentElement.parentElement && e.target.parentElement.parentElement.href) {
  41. const url = e.target.parentElement.parentElement.href;
  42. const processedUrl = processUrl(url);
  43. if (processedUrl) {
  44. e.preventDefault();
  45. window.open(processedUrl, '_blank');
  46. }
  47. }
  48. // 调试打印信息
  49. // console.log(`Original URL: ${url}`);
  50. // console.log(`Processed URL: ${processedUrl}`);
  51. });
  52.  
  53. function processUrl(redirectURL) {
  54. const matches = redirectURL.match(redirectRegex);
  55. console.log(`Matches: ${matches}`);
  56. if (matches && matches[2]) {
  57. return decodeURIComponent(matches[2]);
  58. }
  59. return null;
  60. }
  61. })();
  62.  
  63.  
  64.  

QingJ © 2025

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