webRequest.filterResponseData() 编辑

Use this function to create a webRequest.StreamFilter object for a request. Then use the stream filter to monitor and modify the response. You'd typically call this function from a webRequest event listener.

Firefox uses an optimized byte cache for script requests. This optimized byte cache overrides the normal request caching. Data from this cache is not available in a form useful to extensions. If your extension needs to filter scripts, create your filter in webRequest.onBeforeRequest. Doing this ensures that the filter is created prior to the attempt to load from cache, thereby avoiding the optimized cache.

To use this API you must have the "webRequestBlocking" API permission, as well as the normal permissions needed for the event listener (the "webRequest" permission and the host permission for the host).

Syntax

var filter = browser.webRequest.filterResponseData(
  requestId       // string
)

Parameters

requestId
string. ID of the request to filter. You can get this from the details object that is passed into any webRequest event listeners.

Return value

A webRequest.StreamFilter object that you can use to monitor and modify the response.

Examples

This example, taken from the http-response example extension, creates a filter in webRequest.onBeforeRequest and uses it, to modify the first chunk of the response:

function listener(details) {
  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 {};
}

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

Example extensions

Note that this example only works for small requests that aren't chunked or streamed. More advanced examples are documented with webRequest.StreamFilter.ondata.

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:102 次

字数:4287

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文