0

I'm totally new to javascript and chrome extensions, I'm sorry for any stupid questions.

I'm building a simple chrome extension (no UI) that will replace api.website.com with test.website.com, anytime a link is clicked or url is entered containing api.website.com.

I'm trying to maintain any additional text or slugs after api.website.com too.

For example:

  • api.website.com/testing/slug

becomes

  • test.website.com/testing/slug

I'm trying to use chrome.webRequest.onBeforeRequest.addListener from this page because it seems quick and efficient.

I'm also trying to use parts from each of these pages:

manifest.json

    {
      "name": "Domain Redirect",
      "description": "Replace domain in urls",
      "version": "1.0.0",
      "minimum_chrome_version": "9",
      "manifest_version": 2,
      "background": {
        "scripts": ["background.js"]},
      "permissions": [
        "webRequest",
        "*://api.website.com/"
      ]
    }

background.js

var host = "api.website.com";
chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
         return {redirectUrl: host + details.url.match(/^https?:\/\/[^\/]+([\S\s]*)/)[1]};
    },
    {
        urls: [
            "*://api.website.com/"
        ],
        types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
    },
);

I'm not sure where to put test.website.com in the code or how to proceed with the redirect after replacing the domain in the url. I appreciate any insight anyone's willing to offer.

Beanstalks
  • 21
  • 3
  • See an [example](https://stackoverflow.com/questions/37041743/making-a-chrome-extension-that-replaces-css-and-js-elements/37043831): you need to add `["blocking"]` to the third parameter and webRequestBlocking to `permissions`. – wOxxOm Apr 22 '22 at 17:13

0 Answers0