Dynamic redirects with declarativeNetRequest

Viewed 692

I want to use declarativeNetRequest to redirect all URLs from a domain to a URL with the initiator URL as an encoded parameter e.g.

http://www.google.com --> http://www.newsite.com?url=http%3A%2F%2Fwww.google.com
http://www.google.com?param1=true --> http://www.newsite.com?url=http%3A%2F%2Fwww.google.com%3Fparam1%3Dtrue

Something like...

  {
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "url": `http://www.newsite.com?url=${encodeURIComponent(url)}`
      }
    }
  }

Is this possible to do? If not is there another way to achieve this?

1 Answers
[
  {
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "regexSubstitution": "http://www.newsite.com?url=\\0"
      }
    },
    "condition": {
      "regexFilter": "^https://www.google.com(.*)",
      "resourceTypes": ["main_frame"]
    }
  }
]

It won't URL encode it but I've found it's not really an issue provided the parameter is at the end. Also be careful to limit it to particular sites in the regex or else you could end up with an endless loop.

Related