Korak
Korak

Reputation: 251

chrome.webRequest.onBeforeRequest firing more than once?

I'm trying an experiment to add a variable to a url using chrome's webRequest. The javascript is very simple.

chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
    var url = details.url + '?tag=test';
    return {redirectUrl: url};
  },
{urls: ["<all_urls>"]},
["blocking"]);

However, when I apply it to any domain I'm getting at least a double result of the variable. When I tested with cnn.com using direct nav I actually got the variable appended 4 times.

Here's the relevant snipped of the manifest.

"permissions": [
    "webRequest", "*://*.letags.com/*", "webRequestBlocking"
  ],

Are there round trips happening between the browser and the server so the server is feeding the url back into the extension before actually displaying? If so, how would I structure a match query to recognize when the variable already exists?

Upvotes: 0

Views: 985

Answers (1)

winner_joiner
winner_joiner

Reputation: 14750

This should work:

var url = details.url + (/\?tag=/.test(details.url) ? "" : "?tag=test");
// Explained if details.url contains the text "?tag=" -> don't add Parameter
// Else if it doesn't contain the Parameter -> add it. 

is not beautiful, but works.

Here is a link to a Javascript reference for RegExp.

Upvotes: 1

Related Questions