Repository navigation
Expand file tree
/
Copy pathcontentScript.js
More file actions
347 lines (291 loc) · 8.84 KB
/
Copy pathcontentScript.js
File metadata and controls
347 lines (291 loc) · 8.84 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// contentScript.js
(() => {
try {
const path = window.location.pathname || "";
const BLOCKED_PATHS = [
"/live",
"/friends",
"/following",
"/explore",
"/tiktokstudio"
];
// match exact + subpaths (e.g. /live/abc)
if (BLOCKED_PATHS.some((base) => path === base || path.startsWith(base + "/"))) {
console.log(
"%c[TikTok-Skipper]",
"color:#00c4ff;font-weight:bold",
"Disabled on",
path
);
return;
}
} catch {
return;
}
const VIDEO_SELECTORS = [
'[data-e2e*="feed-video"]',
'[data-e2e*="browse-video"]',
'[data-e2e*="video-card"]',
'[data-e2e*="search-card"]'
].join(",");
const SPONSORED_WORDS = ["sponsored", "promoted"];
const LIVE_WORDS = [" live", "live now"];
const DEFAULT_SETTINGS = {
sponsoredEnabled: true,
liveEnabled: false
};
const settings = {
sponsoredEnabled: DEFAULT_SETTINGS.sponsoredEnabled,
liveEnabled: DEFAULT_SETTINGS.liveEnabled
};
const log = (...args) =>
console.log(
"%c[TikTok-Skipper]",
"color:#00c4ff;font-weight:bold",
...args
);
const isEl = (n) => n && n.nodeType === Node.ELEMENT_NODE;
const getCards = () => Array.from(document.querySelectorAll(VIDEO_SELECTORS));
// state for repeated skipping loop
const skipState = {
active: false,
card: null,
intervalId: null
};
const stopSkipLoop = (reason) => {
if (!skipState.active) return;
try {
clearInterval(skipState.intervalId);
} catch {}
log("Stopping skip loop:", reason);
skipState.active = false;
skipState.card = null;
skipState.intervalId = null;
};
const getCenteredCard = () => {
const cards = getCards();
if (!cards.length) return null;
const midY = window.innerHeight / 2;
let best = null;
let bestDist = Infinity;
for (const card of cards) {
const rect = card.getBoundingClientRect();
if (rect.bottom < 0 || rect.top > window.innerHeight) continue;
const center = rect.top + rect.height / 2;
const dist = Math.abs(center - midY);
if (dist < bestDist) {
bestDist = dist;
best = card;
}
}
return best;
};
const goToNextCard = (currentCard) => {
const cards = getCards();
const idx = cards.indexOf(currentCard);
if (idx === -1) {
log("Could not find current card index, falling back to window scroll.");
window.scrollBy({
top: window.innerHeight * 0.9,
left: 0,
behavior: "smooth"
});
return;
}
const next = cards[idx + 1] || cards[idx - 1];
if (!next) {
log("No next/previous card found, falling back to window scroll.");
window.scrollBy({
top: window.innerHeight * 0.9,
left: 0,
behavior: "smooth"
});
return;
}
log("→ Scrolling to next video card.", {
fromIndex: idx,
toIndex: cards.indexOf(next)
});
next.scrollIntoView({
behavior: "smooth",
block: "center"
});
};
// keep trying to skip every 500ms until a new centered card appears
const startSkipLoop = (card) => {
if (!isEl(card)) return;
if (skipState.active && skipState.card === card) {
// already skipping this card
return;
}
if (skipState.active && skipState.card !== card) {
stopSkipLoop("starting new loop for different card");
}
skipState.active = true;
skipState.card = card;
log("Starting skip loop for blocked card.", card);
skipState.intervalId = setInterval(() => {
const centered = getCenteredCard();
// if some other card is now centered, stop skipping
if (centered && centered !== skipState.card) {
stopSkipLoop("new card centered");
return;
}
// if original card disappeared from DOM, stop
if (!document.body.contains(skipState.card)) {
stopSkipLoop("blocked card removed from DOM");
return;
}
// still the same blocked card, try skipping again
goToNextCard(skipState.card);
}, 500);
};
const getCardText = (card) =>
(card.innerText || card.textContent || "").trim().toLowerCase();
const isSponsoredCard = (card) => {
const text = getCardText(card);
if (!text) return false;
return SPONSORED_WORDS.some((w) => text.includes(w));
};
const isLiveCard = (card) => {
const text = getCardText(card);
if (!text) return false;
if (card.querySelector('[data-e2e*="live"]')) return true;
return LIVE_WORDS.some((w) => text.includes(w));
};
const inspectCard = (card) => {
if (!isEl(card)) return;
if (card.dataset.skipChecked === "1") return;
const sponsored = settings.sponsoredEnabled && isSponsoredCard(card);
const live = settings.liveEnabled && isLiveCard(card);
card.dataset.skipChecked = "1";
if (sponsored || live) {
card.dataset.skipBlocked = "1";
const reason =
sponsored && live
? "SPONSORED + LIVE"
: sponsored
? "SPONSORED"
: "LIVE";
log(
`%cVideo status: ${reason} → skipping`,
"font-weight:bold;color:red",
card
);
// start repeated skip attempts until a new card is centered
startSkipLoop(card);
} else {
log(
"%cVideo status: Allowed (not sponsored/live or feature disabled)",
"font-weight:bold;color:green",
card
);
}
};
const attachObserverToExistingCards = (observer) => {
const cards = document.querySelectorAll(VIDEO_SELECTORS);
cards.forEach((c) => observer.observe(c));
log("Attached observer to", cards.length, "existing video cards.");
};
const setupObservers = () => {
log("Initializing observers…");
const io = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
const card = entry.target;
if (!entry.isIntersecting) return;
if (card.dataset.skipChecked === "1") return;
inspectCard(card);
});
},
{
threshold: 0.6
}
);
attachObserverToExistingCards(io);
const mo = new MutationObserver((mutations) => {
for (const m of mutations) {
m.addedNodes.forEach((n) => {
if (!isEl(n)) return;
if (n.matches && n.matches(VIDEO_SELECTORS)) {
log("New video card detected (root).");
io.observe(n);
}
const nestedCards = n.querySelectorAll
? n.querySelectorAll(VIDEO_SELECTORS)
: [];
nestedCards.forEach((c) => {
log("New video card detected (nested).");
io.observe(c);
});
});
}
});
mo.observe(document.body, { childList: true, subtree: true });
setInterval(() => {
const cards = document.querySelectorAll(VIDEO_SELECTORS);
cards.forEach((card) => {
const rect = card.getBoundingClientRect();
const inView =
rect.top < window.innerHeight * 0.8 && rect.bottom > 0;
if (inView && card.dataset.skipChecked !== "1") {
inspectCard(card);
}
});
}, 2000);
log("Extension active ✔");
};
const loadSettings = (cb) => {
if (!chrome?.storage?.sync) {
log("chrome.storage.sync not available, using defaults.");
cb();
return;
}
chrome.storage.sync.get(DEFAULT_SETTINGS, (res) => {
settings.sponsoredEnabled = !!res.sponsoredEnabled;
settings.liveEnabled = !!res.liveEnabled;
log("Loaded settings:", {
sponsoredEnabled: settings.sponsoredEnabled,
liveEnabled: settings.liveEnabled
});
cb();
});
};
if (chrome?.storage?.onChanged) {
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== "sync") return;
// when toggles change, stop any active skip loop and allow re-checks
if (changes.sponsoredEnabled) {
settings.sponsoredEnabled = !!changes.sponsoredEnabled.newValue;
log("Sponsored toggle changed:", settings.sponsoredEnabled);
stopSkipLoop("settings changed (sponsored)");
document
.querySelectorAll('[data-skip-checked="1"]')
.forEach((el) => delete el.dataset.skipChecked);
}
if (changes.liveEnabled) {
settings.liveEnabled = !!changes.liveEnabled.newValue;
log("Live toggle changed:", settings.liveEnabled);
stopSkipLoop("settings changed (live)");
document
.querySelectorAll('[data-skip-checked="1"]')
.forEach((el) => delete el.dataset.skipChecked);
}
});
}
const boot = () => {
loadSettings(() => {
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
setupObservers();
} else {
window.addEventListener("DOMContentLoaded", setupObservers, {
once: true
});
}
});
};
boot();
})();