const APPLICABLE_PROTOCOLS =["http:","https:"];const PAGE ={UNKNOWN:0,NETFLIX:'Netflix',DISNEY:'Disney'};let enabled ={};/*Get Theme: determines whether to use a light or dark theme*/let theme ='dark';asyncfunctiongetTheme(){let theme ='dark';return theme;}/*Toggle CSS: insert or remove the CSS.Update the page action's title and icon to reflect its state.*/functionenableCSS(tab, page){getTheme().then((theme)=>{
chrome.pageAction.setTitle({tabId: tab.id,title:`Show ${page} UI`});
chrome.tabs.insertCSS({file:"Netflix.css"});
enabled[tab.id] = true;});}functiondisableCSS(tab, page){getTheme().then((theme)=>{
chrome.pageAction.setTitle({tabId: tab.id,title:`Hide ${page} UI`});
chrome.tabs.removeCSS({file:"Netflix.css"});delete enabled[tab.id];});}functiontoggleCSS(tab){const page =urlMatch(tab.url);if(tab.id in enabled){disableCSS(tab, page);}else{enableCSS(tab, page);}}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){if(request.action ==="toggleCSS"){
chrome.tabs.query({active:true,currentWindow:true},function(tabs){const currentTab = tabs[0];toggleCSS(currentTab);sendResponse({status:"success"});});returntrue;}});/*Returns true only if the URL's protocol is in APPLICABLE_PROTOCOLS.Argument url must be a valid URL string.*/functionprotocolIsApplicable(url){const protocol =(newURL(url)).protocol;return APPLICABLE_PROTOCOLS.includes(protocol);}functionurlMatch(url){if(/^https?:\/\/www\.netflix\.com\/?.*/.test(url)){return PAGE.NETFLIX;}if(/^https?:\/\/www\.disneyplus\.com\/?.*/.test(url)){return PAGE.DISNEY;}return PAGE.UNKNOWN;}/*Initialize the page action: set icon and title, then show.Only operates on tabs whose URL's protocol is applicable and url is Netflix or Disney.*/functioninitializePageAction(tab){const page =urlMatch(tab.url);if(protocolIsApplicable(tab.url)&& page != PAGE.UNKNOWN){if(enabled[tab.id]){enableCSS(tab, page);}else{disableCSS(tab, page);}
chrome.pageAction.show(tab.id);}}/*When first loaded, initialize the page action for all tabs.*/
chrome.tabs.query({},function(tabs){for(let tab of tabs){if(tab.url?.startsWith("chrome://"))returnundefined;initializePageAction(tab);}});/*Each time a tab is updated, reset the page action for that tab.*/
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){// Resets the tab's status on a page refreshif(changeInfo.status =="loading"){delete enabled[tabId];}initializePageAction(tab);});
content_script.js
functionrunContentScript(){// Listen for keypressesdocument.addEventListener('keydown',(event)=>{const keyName = event.key;// On right arrow, wind 1 second forward// On z, hide or show the UIif(keyName ==='q'){
chrome.runtime.sendMessage({action:'toggleCSS'});}});}runContentScript();