4chan bypass derefer link interstitial

Bypasses the derefer link interstitial for external links on 4chan

当前为 2022-09-12 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        4chan bypass derefer link interstitial
// @namespace   b3vwq7gg90yplbelsnvz
// @match       https://boards.4chan.org/*
// @match       https://boards.4channel.org/*
// @match       https://sys.4chan.org/derefer
// @match       https://sys.4channel.org/derefer
// @grant       none
// @version     1.0
// @description Bypasses the derefer link interstitial for external links on 4chan
// @run-at      document-start
// @insert-into content
// @license     MIT
// ==/UserScript==

(function () {
	"use strict";

	if (location.hostname.startsWith("sys.4chan")) {
		// Derefer page (somehow), redirect immediately
		const real = new URL(new URLSearchParams(location.search).get("url"));

		if (real && (real.protocol === "https:" || real.protocol === "http:")) {
			const link = document.createElement("a");
			link.rel = "noopener";
			link.referrerPolicy = "no-referrer";
			// Hack fix: 4chan fucks up by double-encoding ampersands
			link.href = String(real).replaceAll("&", "&");
			link.click();
		}
	} else {
		// Board page, fix linkified links
		const dereferLink = "//" + location.host.replace("boards.4chan", "sys.4chan") + "/derefer?url=";
		const derefSelector = `a.linkified[href^="${dereferLink}"]`;

		document.addEventListener("4chanParsingDone", (ev) => {
			const tid = ev.detail?.threadId;
			let context;

			if (tid) {
				context = document.getElementById(`t${tid}`);
			}

			// If we don't find the thread for some reason,
			// just scan the entire page instead idk
			if (!context) {
				context = document.body;
			}


			for (const link of context.querySelectorAll(derefSelector)) {
				let real;

				try {
					// Throws if invalid URL.
					// Use textContent instead of the url parameter
					// because that might have double encoded ampersands
					real = new URL(link.textContent);
				} catch {
					continue;
				}

				if (real.protocol === "https:" || real.protocol === "http:") {
					link.relList.add("noopener");
					link.referrerPolicy = "no-referrer";
					link.href = real;
				}
			}
		});
	}
})();