javascript - Background script being re-run for each page navigated to -


i've written google chrome extension called "word welter" demonstrates typoglycemia.

this extension uses background script toggle word scrambling on , off. however, whilst works on web page, if user turns functionality on , browses different page (i.e. clicks on hyperlink in current page), chrome button goes it's initial state (although button's tooltip text functionality still on!) , scrambling functionality off. looks though content script executed once (i.e. when toggle executed).

i want scrambling functionality remain active user navigates other pages , i'd hoped use of background script work isn't. know how can fix issue?

note scrambling feature worked fine (i.e. every page user navigate to) before introduced toggle button. toggle button need gets annoying if every page scrambled!

the background script (background.js) follows:

var toggle = false; chrome.browseraction.onclicked.addlistener(function(tab) {   toggle = !toggle;   if(toggle){     chrome.browseraction.seticon({path: "wordweltericon19x19_on.png", tabid:tab.id});     chrome.browseraction.settitle({title: "word welter on"});     chrome.tabs.executescript(tab.id, {file:"content_script.js"});   }   else{     chrome.browseraction.seticon({path: "wordweltericon19x19_off.png", tabid:tab.id});     chrome.browseraction.settitle({title: "word welter off"});     chrome.tabs.executescript(tab.id, {code:"window.location.reload();"});   } }); 

and manifest.json has following entry background script:

"background": {    "scripts": ["background.js"]    } 

any appreciated.

i found solution question myself. changed code to:

var toggle = false;  var executecontentscript = function() {     chrome.tabs.executescript({file:"content_script.js", allframes : true }); };  chrome.browseraction.onclicked.addlistener(function(tab) {   toggle = !toggle;   if(toggle){     chrome.browseraction.seticon({path: "wordweltericon19x19_on.png"});     chrome.browseraction.settitle({title: "word welter on"});     executecontentscript();     chrome.tabs.onupdated.addlistener(executecontentscript);   }   else{     chrome.tabs.onupdated.removelistener(executecontentscript);     chrome.browseraction.seticon({path: "wordweltericon19x19_off.png"});     chrome.browseraction.settitle({title: "word welter off"});     chrome.tabs.executescript({code:"window.location.reload();"});   } }); 

the fix add listener content script executed every time active tab updated; see call to:

chrome.tabs.onupdated.addlistener(executecontentscript); 

this listener unregistered when again toggled making call "removelistener" :

chrome.tabs.onupdated.removelistener(executecontentscript); 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -