-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent-script.js
280 lines (246 loc) · 7.72 KB
/
content-script.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
const TIME_OUT_TERM = 3000
const HANDLE_DOM_MUTATIONS_THROTTLE_MS = TIME_OUT_TERM
let domMutationsAreThrottled = false
let hasUnseenDomMutations = false
const API_RETRY_DELAY = 5000
const MAX_RETRIES_PER_THUMBNAIL = 10
let isPendingApiRetry = false
let thumbnailsToRetry = []
let curTheme = 0
const THEME_MODERN = 1
const THEME_CLASSIC = 2
const THEME_GAMING = 3
const THEME_MOBILE = 4
const NUM_THEMES = 4
const isDarkTheme = getComputedStyle(document.body).getPropertyValue('--yt-spec-general-background-a') === ' #181818'
const THUMBNAIL_SELECTORS = []
THUMBNAIL_SELECTORS[THEME_MODERN] = '' + 'a#thumbnail[href]'
THUMBNAIL_SELECTORS[THEME_CLASSIC] = '' +
'.video-thumb' +
':not(.yt-thumb-20)' +
':not(.yt-thumb-27)' +
':not(.yt-thumb-32)' +
':not(.yt-thumb-36)' +
':not(.yt-thumb-48)' +
':not(.yt-thumb-64), ' +
'.thumb-wrapper, ' +
'.pl-header-thumb'
THUMBNAIL_SELECTORS[THEME_GAMING] = '' +
'ytg-thumbnail' +
':not([avatar])' +
':not(.avatar)' +
':not(.ytg-user-avatar)' +
':not(.ytg-box-art)' +
':not(.ytg-compact-gaming-event-renderer)' +
':not(.ytg-playlist-header-renderer)'
THUMBNAIL_SELECTORS[THEME_MOBILE] = '' +
'a.media-item-thumbnail-container, ' +
'a.compact-media-item-image, ' +
'a.video-card-image'
const THUMBNAIL_SELECTOR_VIDEOWALL = '' +
'a.ytp-videowall-still'
const DEFAULT_USER_SETTINGS = {
barPosition: 'top',
barHeight: 4,
barOpacity: 100,
barTooltip: true,
useOnVideoPage: false,
}
let userSettings = DEFAULT_USER_SETTINGS
function delay(ms) {
return new Promise(resolve => setTimeout(resolve,ms));
}
function getNewThumbnails() {
let thumbnails = []
if (curTheme) {
thumbnails = $(THUMBNAIL_SELECTORS[curTheme])
} else {
for (let i = 1; i <= NUM_THEMES; i++) {
thumbnails = $(THUMBNAIL_SELECTORS[i])
if (thumbnails.length) {
curTheme = i
break
}
}
}
thumbnails = $.merge(thumbnails, $(THUMBNAIL_SELECTOR_VIDEOWALL))
return thumbnails
}
function getThumbnailsAndIds(thumbnails) {
const thumbnailsAndVideoIds = []
$(thumbnails).each(function(_, thumbnail) {
let url
if (curTheme === THEME_MODERN) {
url = $(thumbnail).attr('href')
} else if (curTheme === THEME_CLASSIC) {
url = $(thumbnail).attr('href')
|| $(thumbnail).parent().attr('href')
|| $(thumbnail).parent().parent().attr('href')
|| $(thumbnail).children(':first').attr('href')
|| $(thumbnail).children(':first').next().attr('href')
} else if (curTheme === THEME_GAMING) {
url = $(thumbnail).attr('href')
|| $(thumbnail).parent().parent().attr('href')
|| $(thumbnail).parent().parent().parent().attr('href')
if (!$(thumbnail).is('a')) {
thumbnail = $(thumbnail).parent()
}
} else if (curTheme === THEME_MOBILE) {
url = $(thumbnail).attr('href')
const firstChild = $(thumbnail).children(':first')[0]
if ($(firstChild).is('.video-thumbnail-container-compact')) {
thumbnail = firstChild
}
} else {
url = $(thumbnail).attr('href')
}
if (!url) {
return true
}
const previousUrl = $(thumbnail).attr('data-ytrb-processed')
if (previousUrl) {
if (previousUrl === url) {
if (curTheme === THEME_MOBILE) {
if ($(thumbnail).children().last().is('ytrb-bar')) {
return true
}
} else {
return true
}
} else {
$(thumbnail).children('ytrb-bar').remove()
$(thumbnail).removeAttr('data-ytrb-retries')
}
}
$(thumbnail).attr('data-ytrb-processed', url)
const match = url.match(/.*[?&]v=([^&]+).*/)
if (match) {
const id = match[1]
thumbnailsAndVideoIds.push([thumbnail, id])
}
})
return thumbnailsAndVideoIds
}
async function getAdSummary(videoId){
const apiKey = 'key'; // input your api key
const channels = `https://www.googleapis.com/youtube/v3/videos?key=${apiKey}&id=${videoId}&part=snippet`;
const response = await fetch(channels);
const data = await response.json();
return data
}
function getRatingBarHtml(videoId, videoData) {
var color = videoData > 0.5 ? '#b2ffd9' : '#ffa07a';
return getAdSummary(videoId).then(description => {
var ret =
`<ytrb-bar${userSettings.barOpacity !== 100 ? ' style="opacity:' + userSettings.barOpacity / 100 + '"' : ''}>` +
`<ytrb-default style="
display: flex;
height: var(--ytrb-bar-height);
background-color: ${color};
border-radius: 12px 12px 12px 12px;
position: relative;
font-weight: bold;
color: white;
align-items: center;
font-size: 120%;
">
   AD
</ytrb-default>` +
(userSettings.barTooltip
? `<ytrb-tooltip>
<div style="
text-align: center;
font-size: 10px;
overflow-y: scroll;
max-height: 100px;
">${description.items[0].snippet.description}</div>
</ytrb-tooltip>`
: '') +
'</ytrb-bar>';
return ret;
});
}
function addRatingBar(thumbnail, videoId, videoData) {
getRatingBarHtml(videoId, videoData).then(ret => {
$(thumbnail).append(ret)
})
}
function getVideoData(thumbnail, videoId) {
return new Promise(resolve => {
chrome.runtime.sendMessage(
{query: 'videoApiRequest', videoId: videoId},
(isAdIncluded) => {
if (isAdIncluded === null) {
resolve(null)
} else {
resolve(isAdIncluded.flag)
}
}
)
})
}
async function processNewThumbnails() {
const thumbnails = getNewThumbnails()
const thumbnailsAndVideoIds = getThumbnailsAndIds(thumbnails)
for (const [thumbnail, videoId] of thumbnailsAndVideoIds) {
await delay(TIME_OUT_TERM);
getVideoData(thumbnail, videoId).then(videoData => {
if (videoData !== null) {
if (userSettings.barHeight !== 0) {
if(videoData > 0.5){
console.log('Positive AD detected')
addRatingBar(thumbnail, videoId, videoData)
}
else if(videoData >= 0){
console.log('Negative AD detected')
addRatingBar(thumbnail, videoId, videoData)
}
else{
console.log('AD not detected')
}
}
}
})
}
}
function handleDomMutations() {
if (domMutationsAreThrottled) {
hasUnseenDomMutations = true
} else {
domMutationsAreThrottled = true
if (userSettings.barHeight !== 0 || userSettings.showPercentage) {
processNewThumbnails()
}
hasUnseenDomMutations = false
setTimeout(function() {
domMutationsAreThrottled = false
if (hasUnseenDomMutations) {
handleDomMutations()
}
}, HANDLE_DOM_MUTATIONS_THROTTLE_MS)
}
}
const mutationObserver = new MutationObserver(handleDomMutations)
chrome.storage.sync.get(DEFAULT_USER_SETTINGS, function(storedSettings) {
if (storedSettings) {
userSettings = storedSettings
}
const cssFiles = []
cssFiles.push('css/bar.css')
cssFiles.push('css/bar-top.css')
if (userSettings.barTooltip) {
cssFiles.push('css/bar-tooltip.css')
cssFiles.push('css/bar-top-tooltip.css')
}
if (userSettings.useOnVideoPage) {
cssFiles.push('css/bar-video-page.css')
}
if (cssFiles.length > 0) {
chrome.runtime.sendMessage({
query: 'insertCss',
files: cssFiles,
})
}
handleDomMutations()
mutationObserver.observe(document.body, {childList: true, subtree: true})
})