External Link Auto Redirect

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

当前为 2024-04-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name External Link Auto Redirect
  3. // @name:zh-CN 外链自动重定向
  4. // @namespace http://tampermonkey.net/
  5. // @version 1.3.4
  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 httpPattern = /http/g;
  18. const firstHttpExcludeWords = ['portal', 'token', 'sorry', 'qrcode', 'account', 'login', 'sign', 'auth', 'logout', 'register', 'upload', 'share', 'video', 'player', 'play', 'watch', 'stream', 'live', 'api', 'callback'];
  19. const secondHttpExcludeWords = ['.m3u8', '.flv', '.ts']
  20.  
  21.  
  22. function parseUrl(redirectURL) {
  23. let index = findSecondHttpPosition(redirectURL);
  24. if (index !== -1) {
  25. let realUrl = redirectURL.substring(index);
  26. let firstHttp = redirectURL.substring(0, index).toLowerCase();
  27. let secondHttp = realUrl.toLowerCase();
  28.  
  29. for (const ext of firstHttpExcludeWords) {
  30. if (firstHttp.includes(ext)) {
  31. console.log(`firstHttpExcludeWord: ${ext}`);
  32. return null;
  33. }
  34. }
  35.  
  36. for (const ext of secondHttpExcludeWords) {
  37. if (secondHttp.includes(ext)) {
  38. console.log(`secondHttpExcludeWord: ${ext}`);
  39. return null;
  40. }
  41. }
  42.  
  43. realUrl = decodeURIComponent(realUrl);
  44.  
  45. if (isValidUrl(realUrl)) {
  46. return realUrl;
  47. }
  48. }
  49. return null;
  50. }
  51.  
  52. function findSecondHttpPosition(redirectURL) {
  53. let match;
  54. let position = -1;
  55. let count = 0;
  56.  
  57. while ((match = httpPattern.exec(redirectURL)) !== null) {
  58. count++;
  59. if (count === 2) {
  60. position = match.index;
  61. console.log(`Redirect URL: ${redirectURL}`);
  62. return position;
  63. }
  64. }
  65. return -1;
  66. }
  67.  
  68. function isValidUrl(realUrl) {
  69. try {
  70. const url = new URL(realUrl);
  71. return true;
  72. } catch (e) {
  73. return false;
  74. }
  75. }
  76.  
  77. document.addEventListener('click', function (e) {
  78. const element = e.target.closest('a[href]');
  79. if (element) {
  80. const parsedUrl = parseUrl(element.href);
  81. if (parsedUrl) {
  82. e.preventDefault();
  83. window.open(parsedUrl, '_blank');
  84. }
  85. }
  86. });
  87.  
  88. let parsedUrl = parseUrl(window.location.href);
  89. if (parsedUrl) {
  90. window.location.replace(parsedUrl);
  91. }
  92.  
  93. console.log(`parsed URL: ${parsedUrl}`);
  94. })();