-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
48 lines (45 loc) · 1.42 KB
/
Copy pathbackground.js
File metadata and controls
48 lines (45 loc) · 1.42 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
// Initialize video element when a YouTube video page loads
chrome.webNavigation.onCompleted.addListener(async (details) => {
// Only handle main frame navigation to avoid handling iframes
if (details.frameId !== 0) return;
try {
// Inject the initialization script
await chrome.scripting.executeScript({
target: { tabId: details.tabId },
func: () => {
// Function to initialize video element
function initializeVideo() {
const video = document.querySelector('video');
if (video) {
// Force metadata loading
video.preload = 'metadata';
// Try to load metadata without playing
video.load();
return true;
}
return false;
}
// Try immediately
if (!initializeVideo()) {
// If video element is not ready, set up a mutation observer to watch for it
const observer = new MutationObserver((mutations, obs) => {
if (initializeVideo()) {
obs.disconnect(); // Stop observing once we've initialized
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
}
}
});
} catch (error) {
console.error('Error injecting initialization script:', error);
}
}, {
url: [{
hostEquals: 'www.youtube.com',
pathContains: '/watch'
}]
});