Clear all cookies

Tries to recreate going into incognito mode for a website without actually opening an incognito tab

  1. // ==UserScript==
  2. // @name Clear all cookies
  3. // @namespace https://greasyfork.org/en/scripts/522713-clear-all-cookies
  4. // @version 0.2
  5. // @description Tries to recreate going into incognito mode for a website without actually opening an incognito tab
  6. // @author TetteDev
  7. // @license MIT
  8. // @match *://*/*
  9. // @icon https://icons.duckduckgo.com/ip2/tampermonkey.net.ico
  10. // @grant GM_cookie
  11. // @grant GM_registerMenuCommand
  12. // @grant unsafeWindow
  13. // @run-at document-start
  14. // ==/UserScript==
  15.  
  16. const ClearEverything = async (e) => {
  17. const f5KeyCode = 116;
  18. if ((e = e === null ? null : (e || window.event)) !== null // ClearEverything wasnt called from tampermonkey GUI
  19. && !(e.altKey && (e.keyCode == f5KeyCode || e.code === 'F5'))) return;
  20.  
  21. // Gets and clears all present httpOnly cookies for the current document url
  22. const activeCookies = await GM.cookie.list({ url: window.location.href, partitionKey: {} })
  23. .catch(err => { debugger; });
  24. activeCookies.forEach(cookie => {
  25. GM_cookie.delete({ name: cookie.name, url: window.location.href, partitionKey: {} }, function(error) {
  26. if (error) {
  27. debugger;
  28. console.error(error);
  29. }
  30. });
  31. });
  32.  
  33. unsafeWindow.document.cookie.split(';').forEach(cookie => {
  34. const eqPos = cookie.indexOf('=');
  35. const name = eqPos > -1 ? cookie.substring(0, eqPos) : cookie;
  36. document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT';
  37. });
  38. unsafeWindow.caches.keys().then(keys => {
  39. keys.forEach(key => unsafeWindow.caches.delete(key))
  40. });
  41. unsafeWindow.indexedDB.databases().then(dbs => {
  42. dbs.forEach(db => unsafeWindow.indexedDB.deleteDatabase(db.name))
  43. })
  44. unsafeWindow.sessionStorage.clear();
  45. unsafeWindow.localStorage.clear();
  46. location.reload();
  47. };
  48.  
  49. (function() {
  50. 'use strict';
  51. document.addEventListener("keyup", ClearEverything);
  52. GM_registerMenuCommand("Simulate new Session (ALT+F5)", function(event) {
  53. ClearEverything(null);
  54. }, { autoClose: true });
  55. })();