Cat Cleaner+++

gives user possibility to clear website data, directly from page user uses.

  1. // ==UserScript==
  2. // @name Cat Cleaner+++
  3. // @namespace -
  4. // @version 1.1.1
  5. // @description gives user possibility to clear website data, directly from page user uses.
  6. // @author NotYou
  7. // @include *
  8. // @match *://*/*
  9. // @run-at document-body
  10. // @license GPL-3.0-or-later
  11. // @grant GM.registerMenuCommand
  12. // @grant GM.notification
  13. // ==/UserScript==
  14.  
  15. /*
  16.  
  17. ICONS LICENSED UNDER "Linkware" LICENSE
  18.  
  19. BACKLINK: http://www.iconka.com
  20.  
  21. README FILE: https://iconarchive.com/icons/iconka/meow/meow-me.txt
  22.  
  23. */
  24.  
  25. (function() {
  26. const TITLE = 'Cat Cleaner+++'
  27.  
  28. if(!GM.registerMenuCommand) {
  29. warn('No GM.registerMenuCommand, initialization is aborted')
  30.  
  31. return void 0
  32. }
  33.  
  34. const REGISTER_DATA = [
  35. {
  36. label: 'Clear Cookies',
  37. icon: 'https://icons.iconarchive.com/icons/iconka/meow/128/cat-clean-icon.png',
  38. onclick: clearCookies,
  39. text: 'Cookies for that website are cleared.',
  40. },
  41. {
  42. label: 'Clear Local Storage',
  43. icon: 'https://icons.iconarchive.com/icons/iconka/meow/128/cat-walk-icon.png',
  44. onclick: clearLocalStorage,
  45. text: 'Local storage for that website is cleared.',
  46. },
  47. {
  48. label: 'Clear Session Storage',
  49. icon: 'https://icons.iconarchive.com/icons/iconka/meow/128/cat-poo-icon.png',
  50. onclick: clearSessionStorage,
  51. text: 'Session storage for that website is cleared.',
  52. },
  53. {
  54. label: 'Clear Cache',
  55. icon: 'https://icons.iconarchive.com/icons/iconka/meow-2/128/cat-paper-icon.png',
  56. onclick: clearCache,
  57. text: 'Cache storage for that website is cleared.',
  58. },
  59. {
  60. label: 'Clear IndexedDB (Chrome-based browsers only)',
  61. icon: 'https://icons.iconarchive.com/icons/iconka/meow-2/128/cat-paper-icon.png',
  62. onclick: clearIndexedDB,
  63. text: 'Indexed databases for that website are cleared.',
  64. },
  65. {
  66. label: 'Clear Everything',
  67. icon: 'https://icons.iconarchive.com/icons/iconka/meow/128/cat-grumpy-icon.png',
  68. onclick: clearEverything,
  69. text: 'Cookies, local storage, session storage, cache storage, indexed db for that website are cleared.',
  70. }
  71. ]
  72.  
  73. for (let i = 0; i < REGISTER_DATA.length; i++) {
  74. const currentRegisterData = REGISTER_DATA[i]
  75. const { label, icon, onclick, text } = currentRegisterData
  76.  
  77. GM.registerMenuCommand(label, () => {
  78. let validFunctionInitialization = false
  79.  
  80. try {
  81. validFunctionInitialization = onclick()
  82. } catch(e) {
  83. warn(onclick.name + ' function failed initialization, error: ' + e)
  84. }
  85.  
  86. if(validFunctionInitialization) {
  87. if(GM.notification) {
  88. GM.notification({
  89. title: TITLE,
  90. image: icon,
  91. text
  92. })
  93. } else {
  94. warn(text)
  95. }
  96. }
  97. })
  98. }
  99.  
  100. function clearCookies() {
  101. const cookieLength = document.cookie.split(';').length
  102.  
  103. for (let i = 0; i < cookieLength; i++) {
  104. const cookie = document.cookie
  105.  
  106. document.cookie = cookie + ';max-age=0'
  107. }
  108.  
  109. return true
  110. }
  111.  
  112. function clearLocalStorage() {
  113. localStorage.clear()
  114.  
  115. return true
  116. }
  117.  
  118. function clearSessionStorage() {
  119. sessionStorage.clear()
  120.  
  121. return true
  122. }
  123.  
  124. function clearCache() {
  125. caches.keys().then(keyList =>
  126. Promise.all(keyList.map(key =>
  127. caches.delete(key)
  128. )
  129. ))
  130.  
  131. return true
  132. }
  133.  
  134. function clearIndexedDB() {
  135. if(!indexedDB.databases) {
  136. warn('This function is only for Chrome-based browsers!')
  137.  
  138. return false
  139. }
  140.  
  141. indexedDB.databases().then(databases => {
  142. databases.forEach(database => {
  143. indexedDB.deleteDatabase(database)
  144. })
  145. })
  146.  
  147. return true
  148. }
  149.  
  150. function clearEverything() {
  151. clearCookies()
  152. clearLocalStorage()
  153. clearSessionStorage()
  154. clearCache()
  155. clearIndexedDB()
  156.  
  157. return true
  158. }
  159.  
  160. function warn(message) {
  161. alert(TITLE + '\n' + message)
  162. }
  163. })()