javascript - Chrome WebRequest - Add chrome event listener once -
problem
- i'm trying pass headers chrome extension running in tab. @ moment current code, adds 1 event each time page refreshed means if refresh on same page 5 times, end event triggered 5 times on each page load.
restrictions
- i have run tab using background script allow user exclude sites runs on.
- in order specific headers want, have listen using background extension because, attempts re-request resource cors , have access reduced set of headers.
code
chrome.tabs.onupdated.addlistener(function(tabid, changeinfo, tab) { // prevent double firing of event - extension should run once on complete. if (changeinfo.status == "complete") { chrome.webrequest.oncompleted.addlistener( function(details) { console.log(details); // pseudo code, things pulled storage // based on then: executescript(tabid, "built_seo_traffic.js"); }, { urls: ["*://*/*"], types: ["main_frame"], tabid: tab.id }, ["responseheaders"] ); } }); what i've tried
i've tried setting variable checks if event has fired, can script runs once across tabs, rather once on each tab.
am tackling correctly? missing makes easy?
thanks @woxxom managed following solution:
chrome.tabs.onupdated.addlistener(function(tabid, changeinfo, tab) { // prevent double firing of event - extension should run once on complete. if (changeinfo.status == "complete") { chrome.webrequest.oncompleted.addlistener( executetrafficlights, { urls: ["*://*/*"], types: ["main_frame"], tabid: tab.id }, ["responseheaders"] ); } }); executetrafficlights(details){ console.log(details); // pseudo code, things pulled storage // based on then: executescript(tabid, "built_seo_traffic.js"); // remove self after executing chrome.webrequest.oncompleted.removelistener(executetrafficlights); } notably part had been struggling because named function can called without parameters wasn't sure how pass in tabid , tab.
turns out both pieces of information needed (the tabid , url) in details object.
unsure if there other information contained in tab isn't in details, me worked great!
Comments
Post a Comment