Greasy Fork is available in English.

Reset YouTube Settings

Due to YouTube making changes to its layout, some obsolete settings might remain and cause some problems to you. Use this to reset them.

  1. /*
  2.  
  3. MIT License
  4.  
  5. Copyright 2022 CY Fung
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in all
  15. copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24.  
  25. */
  26. // ==UserScript==
  27. // @name Reset YouTube Settings
  28. // @namespace http://tampermonkey.net/
  29. // @version 1.1
  30. // @description Due to YouTube making changes to its layout, some obsolete settings might remain and cause some problems to you. Use this to reset them.
  31. // @author CY Fung
  32. // @supportURL https://github.com/cyfung1031/userscript-supports
  33. // @match https://www.youtube.com/*
  34. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  35. // @grant GM_registerMenuCommand
  36. // @grant GM_addValueChangeListener
  37. // @grant unsafeWindow
  38. // @grant GM.setValue
  39. // @grant GM.deleteValue
  40. // @license MIT
  41. // @require https://cdnjs.cloudflare.com/ajax/libs/js-cookie/3.0.1/js.cookie.min.js#sha512=wT7uPE7tOP6w4o28u1DN775jYjHQApdBnib5Pho4RB0Pgd9y7eSkAV1BTqQydupYDB9GBhTcQQzyNMPMV3cAew==
  42. // ==/UserScript==
  43.  
  44. /* global Cookies, GM_addValueChangeListener, GM_registerMenuCommand */
  45.  
  46. (function () {
  47. 'use strict';
  48. let cb1 = null
  49. GM_registerMenuCommand('Reset YouTube Settings', function () {
  50. if (cb1) return
  51. cb1 = true
  52.  
  53. const whitelist = [
  54. // cookies
  55. 'PREF', 'SID', 'APISID', 'SAPISID', /^__Secure-\w+$/, 'SIDCC',
  56. // localstorage
  57. 'yt-remote-device-id', 'yt-player-headers-readable',
  58. 'ytidb::LAST_RESULT_ENTRY_KEY',
  59. 'yt-remote-connected-devices', 'yt-player-bandwidth',
  60. 'userscript-tabview-settings', // Tabview Youtube
  61. /^[\-\w]*h264ify[\-\w]+$/ // h264ify or enhanced-h264ify
  62. ];
  63.  
  64. const cookiesObject = Cookies.get();
  65. let keysCookies = Object.keys(cookiesObject)
  66. for (const key of keysCookies) {
  67. let value = cookiesObject[key];
  68. if (typeof value !== 'string') continue;
  69. if (whitelist.includes(key)) continue;
  70. let isSkip = false;
  71. for (const s of whitelist) {
  72. if (isSkip) break;
  73. if (typeof s === 'object' && s.constructor.name === 'RegExp') {
  74. if (s.test(key)) isSkip = true;
  75. }
  76. }
  77. if (isSkip) continue;
  78. Cookies.remove(key);
  79. Cookies.remove(key, { domain: 'youtube.com' }); // most youtube cookies use youtube.com
  80. Cookies.remove(key, { domain: 'www.youtube.com' }); // some cookies such as 'WEVNSM' and 'WNMCID' use www.youtube.com
  81. Cookies.remove(key, { secure: true });
  82. Cookies.remove(key, { domain: 'youtube.com', secure: true }); // most youtube cookies use youtube.com
  83. Cookies.remove(key, { domain: 'www.youtube.com', secure: true }); // some cookies such as 'WEVNSM' and 'WNMCID' use www.youtube.com
  84. }
  85.  
  86. const lsObject = localStorage;
  87. let keysLS = Object.keys(lsObject)
  88. for (const key of keysLS) {
  89. let value = lsObject[key];
  90. if (typeof value !== 'string') continue;
  91. if (whitelist.includes(key)) continue;
  92. let isSkip = false;
  93. for (const s of whitelist) {
  94. if (isSkip) break;
  95. if (typeof s === 'object' && s.constructor.name === 'RegExp') {
  96. if (s.test(key)) isSkip = true;
  97. }
  98. }
  99. if (isSkip) continue;
  100. localStorage.removeItem(key);
  101. }
  102.  
  103.  
  104. function getReduceds() {
  105.  
  106. const cookiesObject = Cookies.get();
  107. let keysCookiesNew = Object.keys(cookiesObject)
  108. const lsObject = localStorage;
  109. let keysLSNew = Object.keys(lsObject)
  110.  
  111.  
  112. let reduceds = [keysCookies.length - keysCookiesNew.length, keysLS.length - keysLSNew.length]
  113.  
  114.  
  115. keysCookies = null
  116. keysCookiesNew = null
  117. keysLS = null
  118. keysLSNew = null
  119.  
  120. return reduceds
  121.  
  122. }
  123.  
  124. setTimeout(() => {
  125.  
  126. let reduceds = getReduceds()
  127.  
  128. if (reduceds[0] || reduceds[1]) {
  129. cb1 = () => {
  130. alert(`
  131. ${reduceds[0]} cookies and ${reduceds[1]} localstorages are deleted.
  132. The settings have been reset.
  133.  
  134. Click OK to refresh the browser page.
  135. `.trim())
  136. reduceds = null
  137. unsafeWindow.location.reload()
  138. }
  139. GM.setValue('reset-youtube-settings-flag', Date.now())
  140. } else {
  141. alert('No settings to be required for reset.')
  142. cb1 = null
  143. }
  144.  
  145. }, 300)
  146.  
  147.  
  148. })
  149.  
  150. let triggerOnce = 0
  151. GM_addValueChangeListener('reset-youtube-settings-flag', function (name, old_value, new_value, remote) {
  152. if (!(new_value > 0)) return
  153. let mTriggered = !remote && typeof cb1 === 'function'
  154. const tdt = Date.now();
  155. triggerOnce = tdt
  156. if (mTriggered) {
  157. setTimeout(() => {
  158. if (tdt !== triggerOnce) return
  159. GM.deleteValue('reset-youtube-settings-flag').then(() => {
  160. if (tdt !== triggerOnce) return
  161. setTimeout(() => {
  162. if (tdt !== triggerOnce) return
  163. let pt = Date.now();
  164. window.requestAnimationFrame(() => {
  165. if (tdt !== triggerOnce) return
  166. let ct = Date.now();
  167. if (mTriggered && ct - pt < 800) {
  168. cb1();
  169. cb1 = null
  170. } else {
  171. cb1 = null
  172. unsafeWindow.location.reload()
  173. }
  174. })
  175. }, 30)
  176. })
  177. }, 180)
  178. } else {
  179. if (tdt !== triggerOnce) return
  180. window.requestAnimationFrame(() => {
  181. if (tdt !== triggerOnce) return
  182. cb1 = null
  183. unsafeWindow.location.reload()
  184. })
  185. }
  186. })
  187. })();