-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
112 lines (100 loc) · 3.97 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
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
// Store caption settings and visibility states
let settings = {
captionColor: '#ffff00', // Default color (yellow)
captionSize: 35, // Default size (35px)
showComments: false, // Default: hide comments
showSimilarVideos: false, // Default: hide similar videos
blackBoxOpacity: 0 // Default opacity (0 = fully transparent)
};
// Create a black box element
const createBlackBox = () => {
const blackBox = document.createElement('div');
blackBox.id = 'black-box-overlay';
blackBox.style.position = 'absolute';
blackBox.style.top = '0';
blackBox.style.left = '0';
blackBox.style.width = '100%';
blackBox.style.height = '100%';
blackBox.style.backgroundColor = 'black';
blackBox.style.pointerEvents = 'none'; // Allow clicks to pass through
blackBox.style.zIndex = '9999'; // Ensure it's above the video player
blackBox.style.opacity = settings.blackBoxOpacity;
return blackBox;
};
// Function to update the black box opacity
const updateBlackBox = () => {
let blackBox = document.getElementById('black-box-overlay');
if (!blackBox) {
blackBox = createBlackBox();
const videoPlayer = document.getElementById('movie_player'); // YouTube video player element
if (videoPlayer) {
videoPlayer.style.position = 'relative'; // Ensure the video player can contain the black box
videoPlayer.appendChild(blackBox);
}
}
blackBox.style.opacity = settings.blackBoxOpacity;
};
// Function to change caption styles
const changeCaptions = () => {
const captionElements = document.getElementsByClassName("ytp-caption-segment");
if (captionElements) {
for (let i = 0; i < captionElements.length; i++) {
captionElements[i].style.fontWeight = "500";
captionElements[i].style.background = "rgba(8, 8, 8, 0)";
captionElements[i].style.color = settings.captionColor;
captionElements[i].style.fontSize = `${settings.captionSize}px`;
captionElements[i].style.textTransform = "lowercase";
}
}
};
// Function to hide/show DOM elements
const updateVisibility = () => {
const secondaryColumn = document.getElementById('secondary');
if (secondaryColumn) {
secondaryColumn.style.display = settings.showSimilarVideos ? 'block' : 'none';
}
const comments = document.getElementById('comments');
if (comments) {
comments.style.display = settings.showComments ? 'block' : 'none';
}
};
// Observe changes in the page to detect when the title is available
const observer = new MutationObserver(() => {
changeCaptions(); // Apply caption styles
updateVisibility(); // Apply visibility settings
updateBlackBox(); // Update black box opacity
// top navbar color change
const topbar = document.getElementById('background');
if (topbar) {
topbar.style.background = 'black !important';
}
// background color change
const primary = document.getElementById('columns');
if (primary) {
primary.style.background = 'black';
}
});
// Start observing the body for changes
observer.observe(document.body, { childList: true, subtree: true });
// Listen for messages from the popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'updateSettings') {
// Update stored settings
settings.captionColor = request.captionColor;
settings.captionSize = request.captionSize;
settings.showComments = request.showComments;
settings.showSimilarVideos = request.showSimilarVideos;
settings.blackBoxOpacity = request.blackBoxOpacity;
// Apply changes
changeCaptions();
updateVisibility();
updateBlackBox();
} else if (request.action === 'getSettings') {
// Send current settings back to the popup
sendResponse(settings);
}
});
// Initial check in case relevant elements are already loaded
changeCaptions();
updateVisibility();
updateBlackBox();