javascript - Chrome extension how to detect that domain has changed in specific tab -
i trying detect when specific tab has domain change - example, leave facebook.com go stackoverflow.com. want detect given tab - not if go facebook.com in 1 tab , stackoverflow.com in another. when event happens, need know domain unloaded. if in specific tab navigate facebook.com stackoverflow.com need event trigger , "facebook.com" stored in variable.
i thought following work, tab id changes (and think loading new tab id vs 1 unloaded page??) it's not working. ideas?
in content script:
window.onbeforeunload = function () { var host = location.host; chrome.runtime.sendmessage({unloadevent : host}, function(response){ if(response.issuccess){ console.log('unload message sent'); } }); } in background:
chrome.runtime.onmessage.addlistener( function(request, sender, sendresponse) { console.log('request ', request); ... else if (request.unloadevent){ var queryinfo = { active: true, currentwindow: true }; chrome.tabs.query(queryinfo, function(tabs) { var tab = tabs[0]; var url = tab.url, id = tab.id, host =request.unloadevent; console.log('the current url ' + url + ' , host ' + host); if(!tabs[id]){ console.log("maybe im new page?"); } /*if(tabs[id] && tabs[i] !== request.unloadevent){ console.log("im different site in tab"); }*/ tabs[id] = host; console.log(tabs); sendresponse({issuccess : true}); }); } });
no, sorry. tabid changes when remove/close tab , using content script bad idea.
the right fit use chrome.tabs.onupdated in following:
chrome.tabs.onupdated.addlistener(function(tabid, changeinfo, tab) { if (changeinfo.status == 'complete') { alert('tabid: ' + tabid + 'url: ' + changeinfo.url + 'tab status: ' + changeinfo.status); } }); this must included in background script.
Comments
Post a Comment