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_frame type by definition corresponds top-level frame, in turn means has frameid of 0, not listening sub_frame can 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.

  • type not 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

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -