josephj / ignition-toolbelt

MIT License
0 stars 0 forks source link

[Background] Ability to redirecting a remote production JavaScript URL to a localhost one #7

Open josephj opened 7 months ago

josephj commented 7 months ago

Manifest V3 Example:

For Manifest V3, you would use a similar setup, but with the host_permissions field in the manifest.json:

{
  "manifest_version": 3,
  "name": "URL Redirector",
  "version": "1.0",
  "background": {
    "service_worker": "background.js"
  },
  "permissions": [
    "webRequest"
  ],
  "host_permissions": [
    "*://*.example.com/*"
  ]
}

And the background.js would use the chrome.declarativeNetRequest API instead:

chrome.declarativeNetRequest.onBeforeRequest.addListener(
  function(details) {
    const url = new URL(details.url);
    if (url.hostname === 'www.example.com') {
      const newUrl = url.href.replace('www.example.com', 'localhost:3000');
      return { redirectUrl: newUrl };
    }
  },
  { urls: ["*://www.example.com/*.js"] }, // Pattern to match JS files from example.com
  ["blocking"]
);
josephj commented 4 months ago

It seems the manifest.json v3 has blocked this feature. We probably can only achieve it in DevTools.