Cookie and website data cleaner

Clears cookies and other website data from a list of websites you provide or from every website

当前为 2024-08-14 提交的版本,查看 最新版本

// ==UserScript==
// @name         Cookie and website data cleaner
// @namespace    http://tampermonkey.net/
// @version      1
// @description  Clears cookies and other website data from a list of websites you provide or from every website
// @author       https://gf.qytechs.cn/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() {
	// clearing website storage may prevent certain features from working correctly
	// such as remembering which websites you are logged into, which items are in your basket when shopping etc.

	// start config

	var clearWebsiteDataEveryXMilliseconds = 200;// any number
	var runOnAllWebsites = false;// can be true or false
	var websitesToRunOn = {
		// enter in format 'https://example.com': true,
	};

	// end config

	// only runs on websites that are explicitly allowed to be ran on
	if (!runOnAllWebsites && !websitesToRunOn[location.origin]) {
		return;
	}

	function joinArray(array, seperator, start, end) {
		var joined = '';

		for (var i = start; i < end; i++) {
			joined += array[i] + seperator;
		}

		return joined;
	}

	function clearCookie(cookieName, domain, path) {
		// https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
		// https://stackoverflow.com/questions/5688491/unable-to-delete-cookie-from-javascript
		// can only delete cookie if same domain and path are used as what's already been set

		var pathParts = location.pathname.split(/\//g);

		[location.hostname, location.host].forEach(function(domain) {
			while (domain) {
				for (var i = 0; i < pathParts.length; i++) {
					var path = joinArray(pathParts, '/', 0, i);

					document.cookie = cookieName + '=;' + ' expires=' + new Date(0).toUTCString() + ';' + ' max-age=0;' + ' domain=' + domain + ';' + ' path=' + path + ';';
				}

				domain = domain.replace(/^\.?[^.]+/, '');
			}
		});
	}

	function clearCookies() {	
		var cookieRe = /([^=]+)=[^;]+(?:;\s+|$)/;

		document.cookie.match(new RegExp(cookieRe, 'g') || []).forEach(function(cookie) {
			var cookieName = cookie.match(cookieRe)[1];

			clearCookie(cookieName);
		});

		// console.log(document.cookie);
	}

	function resetWebsiteData() {
		try {
			clearCookies();

			['localStorage', 'sessionStorage'].forEach(function(storage) {
				window[storage].clear();
				console.log(window[storage]);
			});
		}
		catch (err) {
			// page may not have loaded yet or browser doesnt support localStorage or sessionStorage
		}
	}

	setInterval(resetWebsiteData, clearWebsiteDataEveryXMilliseconds);
	resetWebsiteData();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址