Cookie and website data cleaner

Clears cookies and other website data when you go to websites if it isn't allowed to store website data. Edit the code to include websites that can store data.

  1. // ==UserScript==
  2. // @name Cookie and website data cleaner
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Clears cookies and other website data when you go to websites if it isn't allowed to store website data. Edit the code to include websites that can store data.
  6. // @author https://greasyfork.org/en/users/85040-dan-wl-danwl
  7. // @license MIT
  8. // @match *://*/*
  9. // @run-at document-start
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. // MIT License
  14.  
  15. // Copyright(c) 2024 DanWL
  16.  
  17. // Permission is hereby granted, free of charge, to any person obtaining a copy
  18. // of this software and associated documentation files(the "Software"), to deal
  19. // in the Software without restriction, including without limitation the rights
  20. // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
  21. // copies of the Software, and to permit persons to whom the Software is
  22. // furnished to do so, subject to the following conditions:
  23.  
  24. // The above copyright notice and this permission notice shall be included in all
  25. // copies or substantial portions of the Software.
  26.  
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  30. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  32. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. // SOFTWARE.
  34.  
  35. (function() {
  36. // clearing website storage may prevent certain features from working correctly
  37. // such as remembering which websites you are logged into, which items are in your basket when shopping etc.
  38.  
  39. // start config
  40.  
  41. // clearWebsiteDataEveryXMilliseconds takes any number
  42. var clearWebsiteDataEveryXMilliseconds = 200;
  43.  
  44. // runs on websites unless listed here
  45. // made using regular expressions - flags are ignored
  46. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions
  47. // https://regex101.com
  48.  
  49. var websitesToNotRunOn = [
  50. // cookies required for websites to work
  51.  
  52.  
  53. // anything requiring google account:
  54. // /^https:\/\/accounts\.google\.com/,
  55. // google docs:
  56. // /^https:\/\/docs\.google\.com/,
  57. // /^https:\/\/contacts\.google\.com/,
  58. // stay signed into youtube:
  59. // /^https:\/\/accounts\.youtube\.com/,
  60. // /^https:\/\/(www|m)\.youtube\.com/,
  61.  
  62. // stay signed into soundcloud account
  63. // /^https:\/\/secure\.soundcloud\.com/,
  64. // /^https:\/\/soundcloud\.com/,
  65.  
  66. // reddit requires cookies to work even without account
  67. // /^https:\/\/www\.reddit\.com/,
  68.  
  69.  
  70. // cookies that are needed to remember preferences
  71. // /^https:\/\/search\.brave\.com/,
  72. // /^https:\/\/([a-z]+\.)?wikipedia\.org/
  73. ];
  74.  
  75. // end config
  76.  
  77. var websitesToNotRunOnStr = '';
  78.  
  79. websitesToNotRunOn.forEach(function(re, index, arr) {
  80. websitesToNotRunOnStr += '(' + re.source + ')';
  81.  
  82. if ((index + 1 ) < arr.length) {
  83. websitesToNotRunOnStr += '|';
  84. }
  85. });
  86.  
  87. if (location.origin.match(websitesToNotRunOnStr)) {
  88. return;
  89. }
  90.  
  91. function joinArray(array, seperator, start, end) {
  92. var joined = '';
  93.  
  94. for (var i = start; i < end; i++) {
  95. joined += array[i] + seperator;
  96. }
  97.  
  98. return joined;
  99. }
  100.  
  101. function clearCookie(cookieName) {
  102. // https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
  103. // https://stackoverflow.com/questions/5688491/unable-to-delete-cookie-from-javascript
  104. // can only delete cookie if same domain and path are used as what's already been set
  105.  
  106. var pathParts = location.pathname.split(/\//g);
  107. var expires = new Date(0).toUTCString();
  108.  
  109. [location.hostname, location.host].forEach(function(domain) {
  110. while (domain) {
  111. for (var i = 0; i < pathParts.length; i++) {
  112. var path = joinArray(pathParts, '/', 0, i);
  113. var cookie = cookieName + '=; expires=' + expires + '; domain=' + domain + '; path=' + path + ';';
  114.  
  115. if (location.protocol == 'https:') {
  116. cookie += ' SameSite=strict; Secure';
  117. }
  118.  
  119. document.cookie = cookie;
  120. }
  121.  
  122. domain = domain.replace(/^\.?[^.]+/, '');
  123.  
  124. if (!domain.match(/[^.]+\.[^.]+$/)) {
  125. // prevent cookie rejection warning based on invalid domain
  126. break;
  127. }
  128. }
  129. });
  130. }
  131.  
  132. function clearCookies() {
  133. var cookieRe = /([^=]+)=[^;]*(?:;\s+|$)/;
  134.  
  135. document.cookie.match(new RegExp(cookieRe, 'g') || []).forEach(function(cookie) {
  136. var cookieName = cookie.match(cookieRe)[1];
  137.  
  138. clearCookie(cookieName);
  139. });
  140. }
  141.  
  142. function resetWebsiteData() {
  143. try {
  144. clearCookies();
  145.  
  146. ['localStorage', 'sessionStorage'].forEach(function(storage) {
  147. window[storage].clear();
  148. });
  149. }
  150. catch(err) {
  151. // page may not have loaded yet or browser doesnt support localStorage or sessionStorage
  152. }
  153. }
  154.  
  155. setInterval(resetWebsiteData, clearWebsiteDataEveryXMilliseconds);
  156. resetWebsiteData();
  157. })();