-
Notifications
You must be signed in to change notification settings - Fork 12
/
NEN.js
182 lines (160 loc) 路 4.9 KB
/
NEN.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
let options = {};
chrome.runtime.onMessage.addListener(onMessage);
const MAX_TRIES_DISABLE_AUTO_PREVIEW = 5;
const MAX_TRIES_MONITOR_SKIP = 10;
function onMessage(message, sender, sendResponse) {
if (message.action === 'optionsChanged') {
options = message.options;
}
}
$(_ => {
loadOptions(receivedOptions => {
options = receivedOptions;
// It's a react app, so anytime they navigate away or to another title, we need to rehide/do all our options
$('.main-header').on('click', '*', function () {
startHelper();
});
startHelper();
});
});
function dispatchEventToBody(eventType) {
const event = new Event(eventType, {
bubbles: true,
cancelable: false
});
document.dispatchEvent(event);
}
function startMonitoringForSelectors(selectors, numTries) {
if (!selectors.length) {
return;
}
/*Mutation observer for skippable elements*/
const monitor = new MutationObserver(_ => {
let selector = selectors.join(', ');
let elems = document.querySelectorAll(selector);
for (const elem of elems) {
const ariaLabel = elem.getAttribute("aria-label");
const newDataUia = elem.getAttribute("data-uia") || '';
const isCredits = newDataUia.includes('watch-credits');
if (isCredits || newDataUia.includes('next-episode')) {
elem.click();
elem.dispatchEvent(new PointerEvent('click'));
// Send an event that tries to trigger the react version of the action
dispatchEventToBody(isCredits ? 'watchCreditsEvent' : 'nextEpEvent');
} else if (ariaLabel === "Skip Intro") {
doClick(elem).then(_ => {
doGetPlayButton();
});
function doClick(n) {
return new Promise(function (resolve) {
resolve(n.firstChild.click());
});
}
function doGetPlayButton() {
let evt = document.createEvent('Event');
evt.initEvent('playEvent', true, false);
// fire the event
document.dispatchEvent(evt);
}
dispatchEventToBody('skipIntroEvent');
} else {
elem.click();
elem.dispatchEvent(new PointerEvent('click'));
}
}
if (options.disableAutoPlayOnBrowse) {
disableAutoPreview();
}
});
let reactEntry = document.getElementById("appMountPoint");
if (reactEntry) {
/*Start monitoring at react's entry point*/
monitor.observe(reactEntry, {
attributes: false, // Don't monitor attribute changes
childList: true, //Monitor direct child elements (anything observable) changes
subtree: true // Monitor all descendants
});
} else {
if (numTries > MAX_TRIES_MONITOR_SKIP) {
return;
}
numTries++;
setTimeout(_ => {
startMonitoringForSelectors(selectors, numTries);
}, 100 * numTries);
}
}
function startHelper() {
let selectors = [];
if (options.skipTitleSequence) {
enableSkipTitleSequence(selectors);
}
if (options.dontMinimzeEndCreditsOfShow) {
enableDontSkipEndShowCredits(selectors);
}
if (options.autoPlayNext) {
enableAutoPlayNext(selectors);
}
if (options.skipStillHere) {
/* Skip if still watching*/
enableSkipStillHere(selectors);
}
if (options.hideDisliked) {
hideDisliked();
}
if (options.watchCredits) {
enableWatchCredits(selectors);
}
if (options.disableAutoPlayOnBrowse) {
let numTries = 0;
disableAutoPreview(numTries);
}
startMonitoringForSelectors(selectors, 0);
}
function disableAutoPreview(numTries) {
let billboard = document.querySelector('.billboard-row');
if (billboard) {
const parent = billboard.parentElement;
// create padding div to prevent sizing issues
const emptyDiv = document.createElement("div");
emptyDiv.style.height = '30px';
parent.appendChild(emptyDiv);
billboard.remove();
} else {
if (numTries > MAX_TRIES_DISABLE_AUTO_PREVIEW) {
return;
}
setTimeout(_ => {
numTries++;
disableAutoPreview(numTries);
}, numTries * 150);
}
}
function hideDisliked() {
const monitor = new MutationObserver(_ => {
if (window.location.pathname === "/search") {
// Don't hide cards on search page, you might actually be searching for a disliked title
return;
}
let disliked = document.getElementsByClassName("is-disliked");
for (let card of disliked) {
hideSliderItem(card);
}
});
let mainCardView = document.getElementsByClassName("lolomo");
if (mainCardView.length) {
/*Start monitoring at react's entry point*/
monitor.observe(mainCardView[0], {
attributes: false, // Don't monitor attribute changes
childList: true, // Monitor direct child elements (anything observable) changes
subtree: true, // Monitor all descendants
characterData: false // monitor direct text changes
});
}
}
function hideSliderItem(elem) {
let parent = elem.closest(".slider-item");
if (parent) {
parent.style.display = "none";
}
}