Auto-reject cookies and website data

Automatically rejects cookies and other website data or only accepts necessary cookies.

当前为 2024-11-27 提交的版本,查看 最新版本

// ==UserScript==
// @name         Auto-reject cookies and website data
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Automatically rejects cookies and other website data or only accepts necessary cookies.
// @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 = [
		{
			// reject 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',
			manage: 'button.fc-button.fc-cta-manage-options fc-manage-options-thrid-button-label',
			btns: '.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);

			if (item.btn) {
				const btn = document.querySelector(item.btn);

				// some pages would constantly reload because of automatically clearing and rejecting cookies
				// so check if cookies are stored before trying to reject them

				if (btn && document.cookie) {
					// reject cookies
					btn.click();
				}
			}
			else if (item.manage) {
				const manageBtn = document.querySelector(item.manage);

				if (!manageBtn) {
					continue;
				}

				manageBtn.click();

				document.querySelectorAll(item.btns).forEach(function(btn) {
					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();
})();