Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension, onbeforerequest, which page is making the call?

Im making a small google chrome extension that is watching all calls a page is making. The idea is to log how a page behaves and how many external calls are made. Got everything working except the part where i need to get the source url of the page that initiates the call.

For example im going to www.stackoverflow.com in my browser, then my onbeforerequest lister kicks in and gives me all the calls. So far so good. But i still want the name of the page which is making the calls, in this case i want: "www.stackoverflow.com" and the owner of the calls.

I tried getting it from the tabs, but chrome.tabs.get uses a callback and that is not called before its all over and i got all the calls processed.

any ideas on how to get the source url?

edit Im using this code right now, to get the url, but it keeps returning "undefined":

var contentString = "";
chrome.webRequest.onBeforeRequest.addListener(
  function (details) {
      var tabid = details.tabId;      
      var sourceurl = "N/A";
      if (tabid >= 0) {
          chrome.tabs.get(parseInt(tabid), function (tab) {
              sourceurl = tab.url;
              alert(sourceurl);
          });
      }      
      });

When doing the alert, i get undefined for every request

edit 2 - this is working for me

chrome.tabs.get(parseInt(tabid), function (tab) {
              if (tab != undefined) {
                  alert(tab.url);
              }
          });
like image 866
Brian Hvarregaard Avatar asked Feb 28 '13 10:02

Brian Hvarregaard


1 Answers

onBeforeRequest returns a TabID, then you can then use the get method of the tabs API to get a reference to the tab and thus the URL of the page.

like image 55
stuartd Avatar answered Nov 13 '22 21:11

stuartd