Open the F**king URL Right Now

自动跳转某些网站不希望用户直达的外链

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

  1. // ==UserScript==
  2. // @name Open the F**king URL Right Now
  3. // @description 自动跳转某些网站不希望用户直达的外链
  4. // @author OldPanda
  5. // @match http*://c.pc.qq.com/*
  6. // @match http*://iphone.myzaker.com/zaker/link.php?*
  7. // @match http*://link.zhihu.com/?*
  8. // @match http*://t.cn/*
  9. // @match http*://www.360doc.cn/outlink.html?url=*
  10. // @match http://redir.yy.duowan.com/warning.php?url=*
  11. // @match http://www.360doc.com/content/*
  12. // @match https://afdian.net/link?target=*
  13. // @match https://bbs.nga.cn/read.php?*
  14. // @match https://blog.51cto.com/transfer?*
  15. // @match https://developers.weixin.qq.com/community/middlepage/href?href=*
  16. // @match https://docs.qq.com/scenario/link.html?u=*
  17. // @match https://docs.qq.com/scenario/link.html?url=*
  18. // @match https://game.bilibili.com/linkfilter/?url=*
  19. // @match https://gitee.com/link?target=*
  20. // @match https://jump2.bdimg.com/safecheck/index?url=*
  21. // @match https://leetcode.cn/link/?target=*
  22. // @match https://link.csdn.net/?target=*
  23. // @match https://link.juejin.cn/?target=*
  24. // @match https://link.ld246.com/forward?goto=*
  25. // @match https://link.logonews.cn/?*
  26. // @match https://link.uisdc.com/?redirect=*
  27. // @match https://mail.qq.com/cgi-bin/readtemplate*
  28. // @match https://mp.weixin.qq.com/s/*
  29. // @match https://nga.178.com/read.php?*
  30. // @match https://ref.gamer.com.tw/redir.php/url=*
  31. // @match https://shimo.im/outlink/black?url=*
  32. // @match https://sspai.com/link?target=*
  33. // @match https://steamcommunity.com/linkfilter/?url=*
  34. // @match https://steamcommunity.com/linkfilter/?u=*
  35. // @match https://t.me/iv?url=*
  36. // @match https://tieba.baidu.com/mo/q/checkurl?url=*
  37. // @match https://weibo.cn/sinaurl?*
  38. // @match https://weixin110.qq.com/cgi-bin/mmspamsupport-bin/newredirectconfirmcgi*
  39. // @match https://www.bookmarkearth.com/view/*
  40. // @match https://www.chinaz.com/go.shtml?url=*
  41. // @match https://www.coolapk.com/link?url=*
  42. // @match https://www.curseforge.com/linkout?remoteUrl=*
  43. // @match https://www.douban.com/link2/?url=*
  44. // @match https://www.instagram.com/linkshim/?u=*
  45. // @match https://www.jianshu.com/go-wild?*
  46. // @match https://www.kookapp.cn/go-wild.html?url=*
  47. // @match https://www.linkedin.com/safety/go?url=*
  48. // @match https://www.mcbbs.net/plugin.php?id=link_redirect&target=*
  49. // @match https://www.nodeseek.com/jump?to=*
  50. // @match https://www.oschina.net/action/GoToLink?url=*
  51. // @match https://www.pixiv.net/jump.php?*
  52. // @match https://www.qcc.com/web/transfer-link?link=*
  53. // @match https://www.tianyancha.com/security?target=*
  54. // @match https://www.yuque.com/r/goto?url=*
  55. // @match https://xie.infoq.cn/link?target=*
  56. // @exclude https://mp.weixin.qq.com/cgi-bin/*
  57. // @version 1.7.3
  58. // @run-at document-idle
  59. // @namespace https://old-panda.com/
  60. // @require https://cdn.staticfile.org/jquery/3.7.1/jquery.min.js
  61. // @license GPLv3 License
  62. // ==/UserScript==
  63.  
  64. const $ = jQuery.noConflict(true);
  65.  
  66. /**
  67. * @enum {string}
  68. * @name fuckers
  69. * @description all link pattern needed deal with
  70. */
  71. const fuckers = {
  72. afdian: { match: 'https://afdian.net/link?target=', redirect: "target" },
  73. bookmarkearth: { match: 'https://www.bookmarkearth.com/view/', redirect: function () { window.location.replace(document.querySelector("p.link").innerHTML) } },
  74. chinaz: { match: 'https://www.chinaz.com/go.shtml?url=', redirect: "url" },
  75. coolapk: { match: 'https://www.coolapk.com/link?url=', redirect: "url" },
  76. csdn: { match: 'https://link.csdn.net/?target=', redirect: "target" },
  77. cto51: { match: 'https://blog.51cto.com/transfer?', redirect: function () { window.location.href = window.location.href.replace("https://blog.51cto.com/transfer?", "") } },
  78. curseforge: { match: 'https://www.curseforge.com/linkout?remoteUrl=', redirect: function () { window.location.replace(document.querySelector(".root-content a.button").href) } },
  79. dilian: { match: 'https://link.ld246.com/forward?goto=', redirect: "goto" },
  80. doc360_2: { match: 'http://www.360doc.cn/outlink.html?url=', redirect: "url" },
  81. doc360: { match: 'http://www.360doc.com/content/', redirect: function () { $("#articlecontent table tbody tr td#artContent").find("a").off("click") } },
  82. douban: { match: 'https://www.douban.com/link2/?url=', redirect: "url" },
  83. gamebilibili: { match: 'https://game.bilibili.com/linkfilter/?url=', redirect: "url" },
  84. gamertw: { match: 'https://ref.gamer.com.tw/redir.php/?url=', redirect: "url" },
  85. gitee: { match: 'https://gitee.com/link?target=', redirect: "target" },
  86. infoq: { match: 'https://xie.infoq.cn/link?target=', redirect: "target" },
  87. instagram: { match: 'https://www.instagram.com/linkshim/?u=', redirect: "url" },
  88. jianshu: { match: 'https://www.jianshu.com/go-wild?', redirect: "url" },
  89. juejin: { match: 'https://link.juejin.cn/?target=', redirect: "target" },
  90. kook: { match: 'https://www.kookapp.cn/go-wild.html?url=', redirect: "url" },
  91. leetcode: { match: 'https://leetcode.cn/link/?target', redirect: "target" },
  92. linkedin: { match: 'https://www.linkedin.com/safety/go?url=', redirect: "url" },
  93. logonews: { match: 'https://link.logonews.cn/?', redirect: "url" },
  94. mcbbs: { match: 'https://www.mcbbs.net/plugin.php?id=link_redirect&target=', redirect: "target" },
  95. nga: { match: 'https://nga.178.com/read.php?', redirect: function () { $("#m_posts #m_posts_c a").prop("onclick", null).off("click") } },
  96. nga2: { match: 'https://bbs.nga.cn/read.php?', redirect: function () { $("#m_posts #m_posts_c a").prop("onclick", null).off("click") } },
  97. nodeseek: { match: 'https://www.nodeseek.com/jump?to=', redirect: "to" },
  98. oschina: { match: 'https://www.oschina.net/action/GoToLink?url=', redirect: "url" },
  99. pixiv: { match: 'https://www.pixiv.net/jump.php?', redirect: function () { window.location.href = decodeURIComponent(curURL.match(/jump.php\?[url=]*(.*)/)[1].replace(/=$/, '')) } },
  100. qcc: { match: 'https://www.qcc.com/web/transfer-link?link=', redirect: "link" },
  101. qq: { match: 'https://c.pc.qq.com/(middleb|middlem|index).html', redirect: "pfurl", enableRegex: true },
  102. qq2: { match: 'https://c.pc.qq.com/(ios|pc|android).html', redirect: "url", enableRegex: true },
  103. qqdocs: { match: 'https://docs.qq.com/scenario/link.html?url=', redirect: "url" },
  104. qqmail: { match: 'https://mail.qq.com/cgi-bin/readtemplate', redirect: "gourl" },
  105. shimo: { match: 'https://shimo.im/outlink/black', redirect: "url" },
  106. sspai: { match: 'https://sspai.com/link?target=', redirect: "target" },
  107. steam: { match: 'https://steamcommunity.com/linkfilter/?url=', redirect: "url" },
  108. steam2: { match: 'https://steamcommunity.com/linkfilter/?u=', redirect: "u" },
  109. telegram: { match: 'https://t.me/iv?url=', redirect: "url" },
  110. tianyancha: { match: 'https://www.tianyancha.com/security?target=', redirect: "target" },
  111. tieba: { match: 'https://jump2.bdimg.com/safecheck/index?url=', redirect: function () { window.location.replace(document.getElementsByClassName('btn')[0].getAttribute('href')) } },
  112. tieba_2: { match: 'https://tieba.baidu.com/mo/q/checkurl?url=', redirect: "url" },
  113. uisdc: { match: 'https://link.uisdc.com/?redirect=', redirect: "redirect" },
  114. wechat1: { match: 'https://mp.weixin.qq.com/s/', redirect: enableURLs },
  115. wechat2: { match: 'https://weixin110.qq.com/cgi-bin/mmspamsupport-bin/newredirectconfirmcgi', redirect: function () { window.location.replace($(".weui-msg__desc").first().text()) } },
  116. // https://t.cn/RgAKoPE
  117. // https://weibo.cn/sinaurl?luicode=10000011&lfid=230259&u=http%3A%2F%2Ft.cn%2FA6qHeVlf
  118. // https://weibo.cn/sinaurl?toasturl=https%3A%2F%2Ftime.geekbang.org%2F
  119. // https://weibo.cn/sinaurl?u=https%3A%2F%2Fwww.freebsd.org%2F
  120. weibo_1: { match: 'https://t.cn/', redirect: function () { const link = $(".wrap .link").first().text() || document.querySelector('.open-url').children[0].href; window.location.replace(link); } }, // 微博网页版
  121. weibo_2: { match: 'https://weibo.cn/sinaurl?u', redirect: "u" },
  122. weibo_3: { match: 'https://weibo.cn/sinaurl?toasturl', redirect: "toasturl" },
  123. weibo_4: { match: 'https://weibo.cn/sinaurl?', redirect: function () { const link = $(".wrap .link").first().text() || document.querySelector('.open-url').children[0].href; window.location.replace(link); } },
  124. weixindev: { match: 'https://developers.weixin.qq.com/community/middlepage/href?href=', redirect: "href" },
  125. yuque: { match: 'https://www.yuque.com/r/goto?url=', redirect: "url" },
  126. yy: { match: 'http://redir.yy.duowan.com/warning.php?url=', redirect: "url" },
  127. zaker: { match: 'http://iphone.myzaker.com/zaker/link.php?', redirect: function () { redirect(curURL, "url", true) } },
  128. // https://link.zhihu.com/?target=https%3A%2F%2Ftime.geekbang.org%2F
  129. // https://link.zhihu.com/?utm_oi=35221042888704&target=https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/import
  130. zhihu: { match: 'https://link.zhihu.com/?', redirect: "target" },
  131. }
  132.  
  133. const curURL = window.location.href;
  134. const urlPattern = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
  135.  
  136. /**
  137. * Return URL without "http://" or "https://" at the beginning
  138. * @param {String} str
  139. */
  140. function removeProtocol(str) {
  141. return str.replace(/^https?\??:\/\//gm, '');
  142. }
  143.  
  144. function rstrip(str, regex) {
  145. let i = str.length - 1;
  146. while (i >= 0) {
  147. if (!str[i].match(regex)) {
  148. break;
  149. }
  150. i--;
  151. }
  152. return str.substring(0, i + 1);
  153. }
  154.  
  155. /**
  156. * Split concatenated URL string into separate URLs.
  157. * @param {String} str
  158. */
  159. function splitMultiURLs(str) {
  160. //TODO: add comments
  161. let results = new Array();
  162. let entry = "";
  163. while (str.length > 0) {
  164. if (str.indexOf("http:") === -1 && str.indexOf("https:") === -1) {
  165. entry += str;
  166. str = "";
  167. results.push(rstrip(entry, /[@:%_\+~#?&=,$^\*]/g));
  168. break;
  169. }
  170.  
  171. if (str.startsWith("http:")) {
  172. entry += "http:";
  173. str = str.substring("http:".length);
  174. } else if (str.startsWith("https:")) {
  175. entry += "https:";
  176. str = str.substring("https:".length);
  177. } else {
  178. return results;
  179. }
  180.  
  181. let nextIndex = Math.min(
  182. str.indexOf("https:") === -1 ? Number.MAX_SAFE_INTEGER : str.indexOf("https:"),
  183. str.indexOf("http:") === -1 ? Number.MAX_SAFE_INTEGER : str.indexOf("http:")
  184. );
  185. if (nextIndex > 0) {
  186. entry += str.substring(0, nextIndex);
  187. str = str.substring(nextIndex);
  188. }
  189. results.push(rstrip(entry, /[@:%_\+~#?&=,$^\*]/g));
  190. entry = "";
  191. }
  192. return results;
  193. }
  194.  
  195. /**
  196. * Replace url with clickable `<a>` tag in html content.
  197. * @param {String} url
  198. */
  199. function replaceSingleURL(url) {
  200. $("#js_content").html((_, html) => {
  201. return html.replaceAll(url, `<a target="_blank" rel="noopener noreferrer" href="${url}">${url}</a>`);
  202. });
  203. }
  204.  
  205. /**
  206. * Make urls clickable again on Weixin Media Platform.
  207. */
  208. function enableURLs() {
  209. let existingLinks = new Set();
  210. $("a").each(function () {
  211. existingLinks.add(this.href);
  212. });
  213.  
  214. $("#js_content > section").each(function (_, obj) {
  215. // Don't do anything on code blocks
  216. let className = $(obj).attr('class');
  217. if (className != undefined && className.indexOf("code-snippet__js") != -1) {
  218. return;
  219. }
  220. let content = $(obj).text();
  221. let urls = content.matchAll(urlPattern);
  222. let replaced = new Set();
  223. for (let value of urls) {
  224. let urlStr = $.trim(value[0]);
  225. for (let url of splitMultiURLs(urlStr)) {
  226. if (!url || replaced.has(url) || url.includes("localhost") || url.includes("127.0.0.1") || existingLinks.has(url)) {
  227. continue;
  228. }
  229. if (url.endsWith(".") && url[url.length - 2].match(/\d/g)) {
  230. url = url.substring(0, url.length - 2);
  231. }
  232. replaceSingleURL(url);
  233. replaced.add(url);
  234. }
  235. }
  236. });
  237.  
  238. // Replace loading image with actual one
  239. $("img").each(function (_, obj) {
  240. if ($(obj)[0].currentSrc === "data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==") {
  241. $(obj).attr("src", $(obj)[0].dataset.src);
  242. } else {
  243. $(obj).attr("src", $(obj).attr("data-src"));
  244. $(obj).attr("style", "width: 100% !important; height: auto !important; display: initial; visibility: visible !important;");
  245. }
  246. });
  247. $("span.js_img_placeholder").remove();
  248. }
  249.  
  250. function redirect(fakeURLStr, trueURLParam, enableBase64 = false) {
  251. let fakeURL = new URL(fakeURLStr);
  252. let trueURL = fakeURL.searchParams.get(trueURLParam);
  253. if (trueURL.startsWith(fuckers.wechat1.match)) {
  254. // there could be multiple `&`s in url of a wechat link, so all of them
  255. // have to be included in the trueURL.
  256. trueURL = fakeURL.search.split(`${trueURLParam}=`).pop();
  257. } else {
  258. if (enableBase64) trueURL = window.atob(trueURL);
  259. if (trueURL.indexOf("http://") !== 0 && trueURL.indexOf("https://") !== 0) {
  260. trueURL = "https://" + trueURL;
  261. }
  262. }
  263. trueURL = decodeURIComponent(trueURL)
  264. window.location.replace(trueURL);
  265. }
  266.  
  267. /**
  268. * @function
  269. * @name match
  270. * @param {...string} pattern
  271. * @param {...boolean} enableRegex
  272. * @param {...boolean} checkProtocol
  273. * @description check if current URL matchs given patterns
  274. */
  275. function match(pattern, enableRegex = false, checkProtocol = false) {
  276. var curURLProto;
  277. if (checkProtocol) { curURLProto = curURL; }
  278. else {
  279. curURLProto = removeProtocol(curURL);
  280. pattern = removeProtocol(pattern);
  281. }
  282. if (enableRegex) {
  283. return curURLProto.search(pattern) > -1
  284. }
  285. else {
  286. return curURLProto.indexOf(pattern) === 0//Not Sure
  287. }
  288. }
  289.  
  290. (function () {
  291. 'use strict';
  292.  
  293. $(document).ready(function () {
  294. for (var i in fuckers) {
  295. if (match(fuckers[i].match, fuckers[i].enableRegex, fuckers[i].checkProtocol)) {
  296. switch (typeof (fuckers[i].redirect)) {
  297. case 'string':
  298. redirect(curURL, fuckers[i].redirect); break;
  299. case 'function':
  300. fuckers[i].redirect(); break;
  301. default:
  302. console.log(i + " redirect rule error!"); break;
  303. }
  304. }
  305. }
  306. });
  307.  
  308. })();