简体   繁体   中英

Chrome Extension blocking webRequest doesnt seem to be working at all

I am just trying to get the MDN example chrome extension working. here is the manifest.json

{
  "description": "Altering HTTP responses",
  "manifest_version": 2,
  "name": "http-response-filter",
  "version": "1.0",
  "homepage_url":
    "https://github.com/mdn/webextensions-examples/tree/master/http-response",
  "icons": {
    "48": "SA-48x48.png"
  },

  "background": {
    "scripts": ["background.js"]
  }
}

and here is the background.js

function listener(details) {
  console.log("******listen");
  let filter = browser.webRequest.filterResponseData(details.requestId);
  let decoder = new TextDecoder("utf-8");
  let encoder = new TextEncoder();

  filter.ondata = event => {
    let str = decoder.decode(event.data, { stream: true });
    // Just change any instance of Example in the HTTP response
    // to WebExtension Example.
    str = str.replace(/Example/g, "WebExtension Example");
    filter.write(encoder.encode(str));
    filter.disconnect();
  };

  return {};
}
console.log("******");
browser.webRequest.onBeforeRequest.addListener(
  listener,
  { urls: ["https://example.com/*"], types: ["main_frame"] },
  ["blocking"]
);

So i expect it to

  • put a couple console.logs out when i load example.com
  • modify "Example" to "WebExtension Example" as stated by the MDN folks

however, it doesn't work at all for me (I am using Chrome and i added it to my Extensions as an unpacked Extension, i've done other chrome extensions before but this is my first time doing a background script).

Is it possible that something is blocking this background script from running? have i just not configured it in the right way? Please point me in the right direction, thanks very much.

Your Chrome Extension needs to "ask" for permissions to use the webRequest API so that users of the extension will get notified what the extension may be able to do.

Try adding this to your manifest.json :

"permissions": [
    "webRequest",
    "webRequestBlocking"
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM