-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
211 lines (195 loc) · 5.75 KB
/
contentScript.js
File metadata and controls
211 lines (195 loc) · 5.75 KB
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
const BUTTONS = {
autoClickSkip: [
{
selectors: [
'button.button-primary.watch-video--skip-content-button[data-uia="player-skip-intro"]',
{ text: "Skip Intro" },
],
},
],
autoClickSkipRecaps: [
{
selectors: [
"button.button-primary.watch-video--skip-recaps-button",
'button.button-primary.watch-video--skip-content-button[data-uia="player-skip-recap"]',
{ text: "Skip Recap" },
],
},
],
autoClickNext: [
{
selectors: [
'button.color-primary.hasLabel.hasIcon[data-uia="next-episode-seamless-button"]',
'button.hasLabel.hasIcon[data-uia="next-episode-seamless-button-draining"]',
{
selector: "button > span > span",
closest: 'button, [role="button"]',
text: ["Next Episode"],
},
{ text: ["Next Episode"] },
],
platform: ['netflix', 'hotstar']
},
{
selectors: [
'.atv-web-player-next-episode-button-wrapper.button',
'.atvwebpplayersdk-nextupcard-button',
'.atvwebplayersdk-nextupcard-button'
],
platform: ['primevideo']
}
],
};
function findButton(match) {
if (typeof match === "string") {
return document.querySelector(match);
}
if (match && match.selector) {
const elements = Array.from(document.querySelectorAll(match.selector));
for (const element of elements) {
const candidate = match.closest ? element.closest(match.closest) : element;
if (!candidate) {
continue;
}
if (match.text) {
const targetTexts = Array.isArray(match.text)
? match.text.map((text) => text.toLowerCase())
: [match.text.toLowerCase()];
const content =
candidate.textContent && candidate.textContent.toLowerCase();
if (
!content ||
!targetTexts.some((targetText) => content.includes(targetText))
) {
continue;
}
}
return candidate;
}
return null;
}
if (match && match.text) {
const candidates = Array.from(
document.querySelectorAll('button, [role="button"]')
);
const targetTexts = Array.isArray(match.text)
? match.text.map((text) => text.toLowerCase())
: [match.text.toLowerCase()];
return candidates.find((el) => {
const content = el.textContent && el.textContent.toLowerCase();
return (
content &&
targetTexts.some((targetText) => content.includes(targetText))
);
});
}
return null;
}
/**
* Safely call chrome.storage.local.get.
* - If storage API isn't available, calls cb with an empty object.
* - Catches runtime.lastError and exceptions inside the callback.
* - Silently ignores "Extension context invalidated" errors.
*/
function safeStorageGet(keys, cb) {
try {
const hasStorage =
typeof chrome !== "undefined" &&
chrome &&
chrome.storage &&
chrome.storage.local &&
typeof chrome.storage.local.get === "function";
if (!hasStorage) {
cb({});
return;
}
chrome.storage.local.get(keys, (data) => {
try {
if (chrome.runtime && chrome.runtime.lastError) {
const msg = chrome.runtime.lastError.message || "";
if (msg.includes("Extension context invalidated")) {
// Extension unloaded/reloaded — ignore silently.
return;
}
console.error("SkipSter storage error:", chrome.runtime.lastError);
// continue with whatever data we got (or empty)
}
cb(data || {});
} catch (innerErr) {
if (
innerErr &&
innerErr.message &&
innerErr.message.includes("Extension context invalidated")
) {
return;
}
console.error("SkipSter storage callback threw:", innerErr);
cb({});
}
});
} catch (err) {
if (err && err.message && err.message.includes("Extension context invalidated")) {
return;
}
console.error("SkipSter safeStorageGet failed:", err);
cb({});
}
}
function detectAndClick() {
try {
const hostname = window.location.hostname.toLowerCase();
safeStorageGet(
["autoClickNext", "autoClickSkip", "autoClickSkipRecaps"],
(data) => {
try {
for (const [key, selectorArr] of Object.entries(BUTTONS)) {
if (!data[key]) {
continue;
}
if (!Array.isArray(selectorArr)) {
continue;
}
const cusSelector =
selectorArr.find(
(item) =>
Array.isArray(item.platform) &&
item.platform.some((p) => hostname.includes(p))
) || selectorArr.find((item) => !item.platform);
if (!cusSelector) continue;
const selectors = Array.isArray(cusSelector.selectors)
? cusSelector.selectors
: [];
const button = selectors.map(findButton).find(Boolean);
if (button && button instanceof Element) {
if (!button.dataset.skipsterClicked) {
button.dataset.skipsterClicked = "1";
button.click();
}
}
}
} catch (cbErr) {
console.error("SkipSter content script error (callback):", cbErr);
}
}
);
} catch (e) {
console.error("SkipSter content script error:", e);
}
}
const observer = new MutationObserver((mutations) => {
for (let mutation of mutations) {
if (mutation.type !== "childList" || !mutation.addedNodes.length) {
continue;
}
mutation.addedNodes.forEach((node) => {
if (node.nodeType === 1) {
detectAndClick();
}
});
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
detectAndClick();