Daniel
Daniel

Reputation: 1198

WebRequest experimental API access to requests' referrer/superrequest?

I am redirecting WebRequests successfully using the experimental API in Chrome - I am wondering if there is a way to get to the request/URL of the document that caused the URL to be loaded. I.e. the HTML document's URL that triggered, say, the CSS file to load. Something along the lines of the pseudocode:

function onBeforeRequest(details) {
  var incoming = details.url;
  var referrer = referrer_from_details(details);
  var outgoing;
  if(referrer.match(someRE)) {
    outgoing = "one place";
  } else {
    outgoing = "somewhere else";
  }
  return { redirectUrl: outgoing };
}

What I am missing is the referrer_from_details function. The closest thing I could find was to go via tab/frame IDs to get to the URL, but not only did that seem like the wrong way around, it also is asynchronous (AFAIK).

Anybody know how to get the referrer out?

Upvotes: 1

Views: 550

Answers (1)

albinowax
albinowax

Reputation: 68

You can access the Referer header in details.requestHeaders:

function(details) {
  for (var i = 0; i < details.requestHeaders.length; ++i) {
  if (details.requestHeaders[i].name === 'Referer') {
       alert(details.requestHeaders[i].value);
       break;
  }
}
return {requestHeaders: details.requestHeaders};
}

Upvotes: 1

Related Questions