Greasy Fork

Rock Paper Shotgun [RPS] & Eurogamer embed fix

Enable embedded third party content on RPS & Eurogamer despite having rejected cookies

目前为 2023-12-01 提交的版本。查看 最新版本

// ==UserScript==
// @name         Rock Paper Shotgun [RPS] & Eurogamer embed fix
// @version      8
// @description  Enable embedded third party content on RPS & Eurogamer despite having rejected cookies
// @author       Tim Smith
// @license      GPL-3.0 License
// @namespace    https://greasyfork.org/users/945293
// @match        *://www.rockpapershotgun.com/*
// @match        *://www.eurogamer.net/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=rockpapershotgun.com
// ==/UserScript==

(function() {

class EmbedFixer {
	sourcesByDataName = [
		['src', [
			['iframe', 'embed.acast.com'],
			['script', 'apester.com'],
			['iframe', 'bandcamp.com'],
			['iframe', 'efmfeedback.com'],
			['iframe', 'docs.google.com'],
			['iframe', 'gfycat.com'],
			['iframe', 'giphy.com'],
			['iframe', 'libsyn.com'],
			['iframe', 'megaphone.fm'],
			['iframe', 'redditmedia.com'],
			['iframe', 'soundcloud.com'],
			['iframe', 'spotify.com'],
			['iframe', 'streamable.com'],
			['iframe', 'twitch.tv'],
			['iframe', 'youtube']
		]],
		['cookiesSrc', [
			['script', 'primis.tech']
		]]
	]
	mutationObserverOptions = {
		childList: true,
		subtree: true
	}

	constructor() {
		this.selectors = this.sourcesByDataName.map(([dataName, sources]) => [dataName, sources.map(source => this.selector(dataName, ...source)).join()]);
		this.twitterScriptAdded = false;

		this.enable(document.body);

		const liveblogContainer = document.body.querySelector('.liveblog');
		if (liveblogContainer !== null) {
			this.mutationObserver = new MutationObserver(mutationRecords => this.checkMutations(mutationRecords));
			this.mutationObserver.observe(liveblogContainer, this.mutationObserverOptions);
		}
	}

	selector(dataName, nodeName, value) {
		return `${nodeName}[data-${this.dashStyle(dataName)}*="${value}"]`;
	}

	dashStyle(camelCase) {
		return camelCase.replaceAll(/[A-Z]/g, letter => `-${letter.toLowerCase()}`);
	}

	enable(ancestor) {
		for (const [dataName, selector] of this.selectors)
			ancestor.querySelectorAll(selector).forEach(element => element.src = element.dataset[dataName]);

		if (!this.twitterScriptAdded && ancestor.querySelector('blockquote.twitter-tweet') !== null)
			this.addTwitterScript();
	}

	addTwitterScript() {
		const widgetScript = document.createElement('script');
		widgetScript.async = true;
		widgetScript.src = 'https://platform.twitter.com/widgets.js';
		widgetScript.charset = 'utf-8';
		document.head.appendChild(widgetScript);
		this.twitterScriptAdded = true;
	}

	checkMutations(mutationRecords) {
		for (const mutationRecord of mutationRecords)
			for (const addedNode of mutationRecord.addedNodes)
				if (addedNode.nodeType == Node.ELEMENT_NODE)
					this.enable(addedNode);
	}
}

const embedFixer = new EmbedFixer();

})();