-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
183 lines (142 loc) · 3.96 KB
/
background.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
const BADGED_TYPES = [
'videoRenderer',
'compactVideoRenderer',
'channelRenderer',
'richItemRenderer',
];
const decoder = new TextDecoder("utf-8");
const encoder = new TextEncoder();
let deverifyIsEnabled = false;
(function boot() {
(async function () {
deverifyIsEnabled = Boolean(
(await browser.storage.local.get('deverifyIsEnabled'))?.deverifyIsEnabled
)
})();
browser.webRequest.onBeforeRequest.addListener(
(details) => {
const url = new URL(details.url);
if (/\/youtubei\/v1\//.test(url.pathname)) {
onAPIRequest(details);
}
},
{
urls: ['https://www.youtube.com/*'],
types: ['xmlhttprequest'],
},
['blocking']
);
browser.webRequest.onBeforeRequest.addListener(
onResultsSSR,
{
urls: ['https://www.youtube.com/*'],
types: ['main_frame', 'sub_frame', 'object'],
},
['blocking']
);
async function updateStatus () {
deverifyIsEnabled = !deverifyIsEnabled;
await browser.storage.local.set({ deverifyIsEnabled });
updateIcon(deverifyIsEnabled);
};
browser.browserAction.onClicked.addListener(updateStatus);
updateStatus();
})();
function onAPIRequest(details) {
const filter = browser.webRequest.filterResponseData(details.requestId);
let body = '';
filter.ondata = (event) => {
body += decoder.decode(event.data, {stream: true});
};
filter.onstop = (event) => {
try {
const results = JSON.parse(body);
if (deverifyIsEnabled) {
deverifyResults(results);
}
filter.write(
encoder.encode(JSON.stringify(results))
);
} finally {
filter.disconnect();
}
};
}
function onResultsSSR(details) {
const filter = browser.webRequest.filterResponseData(details.requestId);
let body = '';
filter.ondata = (event) => {
body += decoder.decode(event.data, {stream: true});
};
filter.onstop = (event) => {
try {
const parser = new DOMParser();
const doc = parser.parseFromString(body, 'text/html');
if (deverifyIsEnabled) {
const scripts = doc.getElementsByTagName('script');
for (const script of scripts) {
const ytInitialData =
script.textContent?.match(/^\s*var\s*ytInitialData\s*=\s*(.*);/m)?.[1];
if (!ytInitialData) continue;
const results = JSON.parse(ytInitialData);
deverifyResults(results);
script.textContent = `var ytInitialData = ${JSON.stringify(results)};`;
}
}
filter.write(
encoder.encode(doc.documentElement.outerHTML)
);
} finally {
filter.disconnect();
}
};
}
function deverifyResults(results) {
if (typeof results !== 'object' || results === null) return;
for (const key in results) {
const value = results[key];
if (!Array.isArray(value)) {
deverifyResults(value);
continue;
}
const filteredCollection = [];
for (const item of value) {
if (
item.shelfRenderer ||
item.horizontalCardListRenderer
) continue;
if (
!item ||
!BADGED_TYPES.some(type => item[type])
) {
deverifyResults(item);
filteredCollection.push(item);
continue;
}
const type = Object.keys(item)[0];
const isVerified =
getIsVerified(item[type]) ||
getIsVerified(
item[type].content?.[Object.keys(item[type].content)]
);
if (!isVerified)
filteredCollection.push(item);
}
results[key] = filteredCollection;
}
}
function getIsVerified(item) {
return Boolean(
item?.ownerBadges?.some(badge =>
badge.metadataBadgeRenderer?.style === 'BADGE_STYLE_TYPE_VERIFIED' ||
badge.metadataBadgeRenderer?.style === 'BADGE_STYLE_TYPE_VERIFIED_ARTIST'
)
);
}
function updateIcon(isEnabled) {
if (isEnabled) {
browser.browserAction.setIcon({ path: 'icons/enabled.png' });
} else {
browser.browserAction.setIcon({ path: 'icons/disabled.png' });
}
}