LiveOverlayExtension/background.js
TristanEDU a34b719eaf feat: add toggle-based overlay activation per tab via extension icon
- Removed automatic content script injection from manifest
- Added background script with click handler for toggling overlay
- Overlay is now injected only when extension icon is clicked
- Overlay can be removed by clicking the icon again
- Prevents unintended injection across multiple tabs of same domain
- Per-tab state tracking for clean UX
2025-05-30 13:44:35 -07:00

31 lines
926 B
JavaScript

const tabOverlayStatus = {};
chrome.action.onClicked.addListener(async (tab) => {
const tabId = tab.id;
if (tabOverlayStatus[tabId]) {
// Already injected → remove overlay
chrome.scripting.executeScript({
target: { tabId: tabId },
func: () => {
const iframe = document.getElementById("overlay-iframe");
const panel = document.querySelector(
'div[style*="Overlay Controls"]'
)?.parentElement;
if (iframe) iframe.remove();
if (panel) panel.remove();
},
});
tabOverlayStatus[tabId] = false;
chrome.action.setIcon({ tabId, path: "icon-off.png" }); // optional
} else {
// Not injected → inject content.js
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ["content.js"],
});
tabOverlayStatus[tabId] = true;
chrome.action.setIcon({ tabId, path: "icon-on.png" }); // optional
}
});