Auto-reject cookies and website data

Automatically rejects cookies and legitimate interest

目前為 2024-11-27 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Auto-reject cookies and website data
// @namespace    http://tampermonkey.net/
// @version      0.2.4
// @description  Automatically rejects cookies and legitimate interest
// @author       https://greasyfork.org/en/users/85040-dan-wl-danwl
// @license      MIT
// @match        *://*/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

// MIT License

// Copyright(c) 2024 DanWL

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// 	in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

(function() {
	const rejections = [
		{
			// only accept necessary cookies
			banner: 'body > #onetrust-consent-sdk',
			btn: 'body > #onetrust-consent-sdk > [role="region"][aria-label="Cookie banner"] #onetrust-reject-all-handler'
		},
		{
			// reject consent and reject legitimate interest
			banner: 'body > .fc-consent-root',
			btn: 'body > .fc-consent-root button[aria-label^="Manage"] > p',
			btns: 'body > .fc-consent-root .fc-preference-slider input[aria-label^="Consent"][aria-pressed="true"], .fc-preference-slider input[aria-label^="Legitimate interest"][aria-pressed="true"]'
		}
	];

	function rejectAll() {
		for (let i = 0; i < rejections.length; i++) {
			const item = rejections[i];
			const banner = document.querySelector(item.banner);
			const btn = document.querySelector(item.btn);

			if (!btn) {
				continue;
			}

			if (item.btns) {
				btn.click();

				document.querySelectorAll(item.btns).forEach(function(toggle) {
					toggle.click();
				});
			}
			else {
				// some pages would constantly reload because of automatically clearing and rejecting cookies
				// so check if cookies are stored before trying to reject them

				if (document.cookie) {
					// reject cookies
					btn.click();
				}
			}

			if (banner) {
				// make sure there is no persistent banner
				banner.outerHTML = '';
			}
		}

		// cookie banner may not have loaded yet, try again
		setTimeout(rejectAll, 200);
	}

	rejectAll();
})();