-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.js
More file actions
71 lines (61 loc) · 2.67 KB
/
Copy pathpopup.js
File metadata and controls
71 lines (61 loc) · 2.67 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
const toggle = document.getElementById("toggle-switch");
const statusMessage = document.getElementById("video-status");
// Check and apply the saved state on page load
window.addEventListener('load', () => {
const savedState = localStorage.getItem("videoToggleState") === "true"; // Get saved state from localStorage
toggle.checked = savedState; // Set the toggle switch accordingly
statusMessage.textContent = savedState ? "hidden" : "visible"; // Update the status message
// Ensure the video visibility is updated based on saved state
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tab = tabs[0];
if (tab && (tab.url.includes("youtube.com") || tab.url.includes("music.youtube.com"))) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: toggleVideo,
args: [savedState] // Apply the saved state to the video visibility
});
}
});
});
// Event listener for toggle change
toggle.addEventListener("change", async () => {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab && (tab.url.includes("youtube.com") || tab.url.includes("music.youtube.com"))) {
const isChecked = toggle.checked;
// Save the current toggle state to localStorage
localStorage.setItem("videoToggleState", isChecked);
// Execute the toggleVideo function to hide or show the video
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: toggleVideo,
args: [isChecked]
});
// Update the status message accordingly
statusMessage.textContent = isChecked ? "hidden" : "visible";
} else {
alert("This extension only works on YouTube and YouTube Music.");
toggle.checked = false; // Reset toggle if not on YouTube
localStorage.setItem("videoToggleState", "false"); // Ensure state is reset
}
});
// Function to toggle the video visibility
function toggleVideo(hide) {
const overlayText = "🔘🌐Built with love ❤️ by Supratim";
const videoElements = document.querySelectorAll("video");
videoElements.forEach((video) => {
// Hide or show the video based on the state
video.style.visibility = hide ? "hidden" : "visible";
let overlay = video.parentElement.querySelector(".custom-overlay");
if (hide) {
if (!overlay) {
overlay = document.createElement("div");
overlay.className = "custom-overlay";
overlay.textContent = overlayText;
video.parentElement.appendChild(overlay);
}
overlay.style.display = "flex"; // Show overlay when video is hidden
} else if (overlay) {
overlay.style.display = "none"; // Hide overlay when video is visible
}
});
}