4chan bypass derefer link interstitial

Bypasses the derefer link interstitial for external links on 4chan

目前為 2022-09-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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;
				}
			}
		});
	}
})();