-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreset-youtube-settings.user.js
187 lines (159 loc) · 6.21 KB
/
reset-youtube-settings.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
MIT License
Copyright 2022 CY Fung
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.
*/
// ==UserScript==
// @name Reset YouTube Settings
// @namespace http://tampermonkey.net/
// @version 1.1
// @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.
// @author CY Fung
// @supportURL https://github.com/cyfung1031/userscript-supports
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant GM_registerMenuCommand
// @grant GM_addValueChangeListener
// @grant unsafeWindow
// @grant GM.setValue
// @grant GM.deleteValue
// @license MIT
// @require https://cdnjs.cloudflare.com/ajax/libs/js-cookie/3.0.1/js.cookie.min.js#sha512=wT7uPE7tOP6w4o28u1DN775jYjHQApdBnib5Pho4RB0Pgd9y7eSkAV1BTqQydupYDB9GBhTcQQzyNMPMV3cAew==
// ==/UserScript==
/* global Cookies, GM_addValueChangeListener, GM_registerMenuCommand */
(function () {
'use strict';
let cb1 = null
GM_registerMenuCommand('Reset YouTube Settings', function () {
if (cb1) return
cb1 = true
const whitelist = [
// cookies
'PREF', 'SID', 'APISID', 'SAPISID', /^__Secure-\w+$/, 'SIDCC',
// localstorage
'yt-remote-device-id', 'yt-player-headers-readable',
'ytidb::LAST_RESULT_ENTRY_KEY',
'yt-remote-connected-devices', 'yt-player-bandwidth',
'userscript-tabview-settings', // Tabview Youtube
/^[\-\w]*h264ify[\-\w]+$/ // h264ify or enhanced-h264ify
];
const cookiesObject = Cookies.get();
let keysCookies = Object.keys(cookiesObject)
for (const key of keysCookies) {
let value = cookiesObject[key];
if (typeof value !== 'string') continue;
if (whitelist.includes(key)) continue;
let isSkip = false;
for (const s of whitelist) {
if (isSkip) break;
if (typeof s === 'object' && s.constructor.name === 'RegExp') {
if (s.test(key)) isSkip = true;
}
}
if (isSkip) continue;
Cookies.remove(key);
Cookies.remove(key, { domain: 'youtube.com' }); // most youtube cookies use youtube.com
Cookies.remove(key, { domain: 'www.youtube.com' }); // some cookies such as 'WEVNSM' and 'WNMCID' use www.youtube.com
Cookies.remove(key, { secure: true });
Cookies.remove(key, { domain: 'youtube.com', secure: true }); // most youtube cookies use youtube.com
Cookies.remove(key, { domain: 'www.youtube.com', secure: true }); // some cookies such as 'WEVNSM' and 'WNMCID' use www.youtube.com
}
const lsObject = localStorage;
let keysLS = Object.keys(lsObject)
for (const key of keysLS) {
let value = lsObject[key];
if (typeof value !== 'string') continue;
if (whitelist.includes(key)) continue;
let isSkip = false;
for (const s of whitelist) {
if (isSkip) break;
if (typeof s === 'object' && s.constructor.name === 'RegExp') {
if (s.test(key)) isSkip = true;
}
}
if (isSkip) continue;
localStorage.removeItem(key);
}
function getReduceds() {
const cookiesObject = Cookies.get();
let keysCookiesNew = Object.keys(cookiesObject)
const lsObject = localStorage;
let keysLSNew = Object.keys(lsObject)
let reduceds = [keysCookies.length - keysCookiesNew.length, keysLS.length - keysLSNew.length]
keysCookies = null
keysCookiesNew = null
keysLS = null
keysLSNew = null
return reduceds
}
setTimeout(() => {
let reduceds = getReduceds()
if (reduceds[0] || reduceds[1]) {
cb1 = () => {
alert(`
${reduceds[0]} cookies and ${reduceds[1]} localstorages are deleted.
The settings have been reset.
Click OK to refresh the browser page.
`.trim())
reduceds = null
unsafeWindow.location.reload()
}
GM.setValue('reset-youtube-settings-flag', Date.now())
} else {
alert('No settings to be required for reset.')
cb1 = null
}
}, 300)
})
let triggerOnce = 0
GM_addValueChangeListener('reset-youtube-settings-flag', function (name, old_value, new_value, remote) {
if (!(new_value > 0)) return
let mTriggered = !remote && typeof cb1 === 'function'
const tdt = Date.now();
triggerOnce = tdt
if (mTriggered) {
setTimeout(() => {
if (tdt !== triggerOnce) return
GM.deleteValue('reset-youtube-settings-flag').then(() => {
if (tdt !== triggerOnce) return
setTimeout(() => {
if (tdt !== triggerOnce) return
let pt = Date.now();
window.requestAnimationFrame(() => {
if (tdt !== triggerOnce) return
let ct = Date.now();
if (mTriggered && ct - pt < 800) {
cb1();
cb1 = null
} else {
cb1 = null
unsafeWindow.location.reload()
}
})
}, 30)
})
}, 180)
} else {
if (tdt !== triggerOnce) return
window.requestAnimationFrame(() => {
if (tdt !== triggerOnce) return
cb1 = null
unsafeWindow.location.reload()
})
}
})
})();