JxxIT
JxxIT

Reputation: 1

Chrome Extension Inject Script Into Error Page

I have a router that uses HTTP, but it redirects me to HTTPS. On the HTTPS site it refuses the connection. I have a fix: if I go back to the previous page, it loads HTTP correctly. How can I make a chrome extension that does this?

I already have a manifest:

{
  "manifest_version": 3,
  "name": "Zyxel Blocker",
  "description": "Blocks your Zyxel Router from redirecting you to HTTPS",
  "version": "1.0",
  "permissions": ["webNavigation", "tabs", "scripting"],
  "background": {
    "service_worker": "background.js"
  },
  "host_permissions": ["*://192.168.0.1/*"],
  "content_scripts": [
    {
      "matches": ["*://192.168.0.1/*"],
      "js": ["content.js"]
    }
  ]
}

background.js:

chrome.webNavigation.onErrorOccurred.addListener((details) => {
  if (details.error === "net::ERR_CONNECTION_REFUSED") {
    chrome.scripting.executeScript({
      target: { tabId: details.tabId },
      files: ["content.js"],
    });
  }
});

content.js:

window.history.back();

But now it gives the error: Uncaught (in promise) Error: Frame with ID 0 is showing error page

How can I fix my code or is there another way to do this?

Thanks, JxxIT

Upvotes: 0

Views: 756

Answers (1)

Torben
Torben

Reputation: 549

You can not inject scripts into the error page in Chrome but there is another way to do what you want. Inside your webNavigation listener you can trigger a chrome.tabs.update() to redirect to your http site like this:

chrome.tabs.update( details.tabId, {
  url: "http://website-url"
} );

Tip: Use details.url to get the requested URL.

Upvotes: 0

Related Questions