get the document url in chrome.webRequest.onBeforeRequest -
in chrome.webrequest.onbeforerequest sorts of url's -- javascript, css etc.
for every url want know main tab url.
what simplest way synchronously?
right i'm using way:
if details.frameid == 0 details.url contains main tab url tab id
chrome.webrequest.onbeforerequest.addlistener( function (details) { if (details.tabid == -1) { return; } if ("type" in details && ['main_frame', 'sub_frame'].indexof(details.type) !== -1) { if (details.frameid == 0) { all_tabs_info.add_tab_info(details.tabid, details.url); } } }, { urls: ['<all_urls>'] }, ["blocking"]); onwards if request comes tab id have tab url. crude logic seems working.
a couple of enhancements:
- use filters as possible, in case
type. main_frametype by definition corresponds top-level frame, in turn means hasframeidof 0, not listeningsub_framecan omit check altogether.the value 0 indicates request happens in main frame; positive value indicates id of subframe in request happens. if document of (sub-)frame loaded (type main_frame or sub_frame), frameid indicates id of frame, not id of outer frame. frame ids unique within tab.
typenot optional can see in documentation, no need doubt presence.
so simplified code this:
chrome.webrequest.onbeforerequest.addlistener( function(details) { if (details.tabid >= 0) { all_tabs_info.add_tab_info(details.tabid, details.url); } }, { urls: ['<all_urls>'], types: ['main_frame'], }, ["blocking"] );
Comments
Post a Comment