-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
69 lines (59 loc) · 2.14 KB
/
content.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
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach((node) => {
if (node.tagName === 'YTD-PLAYER') {
checkYouTubeShort(node);
}
});
}
});
});
observer.observe(document.body, { childList: true, subtree: true });
function checkYouTubeShort(playerElement) {
const videoId = getVideoIdFromUrl(window.location.href);
if (videoId) {
chrome.runtime.sendMessage({action: 'analyzeYouTubeShort', videoId: videoId}, (response) => {
if (response && response.block) {
blockYouTubeShort(playerElement, response.reason);
}
});
} else {
console.warn('No valid video ID found, skipping analysis');
}
}
function getVideoIdFromUrl(url) {
const patterns = [
/(?:\/shorts\/|\/watch\?v=|\/embed\/|\/v\/|\/youtu.be\/)([a-zA-Z0-9_-]{11})/,
/^([a-zA-Z0-9_-]{11})$/
];
for (let pattern of patterns) {
const match = url.match(pattern);
if (match && match[1]) {
return match[1];
}
}
console.warn('Could not extract video ID from URL:', url);
return null;
}
function blockYouTubeShort(playerElement, reason) {
playerElement.style.display = 'none';
const message = document.createElement('div');
message.textContent = `This Short has been blocked: ${reason}`;
message.style.cssText = 'padding: 20px; text-align: center; background: #f0f0f0;';
playerElement.parentNode.insertBefore(message, playerElement);
const skipButton = document.createElement('button');
skipButton.textContent = 'Next Short';
skipButton.onclick = () => {
const event = new KeyboardEvent('keydown', {'keyCode': 39, 'which': 39});
document.dispatchEvent(event);
};
message.appendChild(skipButton);
const overrideButton = document.createElement('button');
overrideButton.textContent = 'Watch Anyway';
overrideButton.onclick = () => {
message.remove();
playerElement.style.display = '';
};
message.appendChild(overrideButton);
}