webextensions-examples icon indicating copy to clipboard operation
webextensions-examples copied to clipboard

browser.webRequest.filterResponseData ondata error

Open admimistrator opened this issue 7 years ago • 3 comments

https://github.com/mdn/webextensions-examples/tree/master/http-response code :

function listener(details) {
  var filter = browser.webRequest.filterResponseData(details.requestId);
  var decoder = new TextDecoder("utf-8");
  var encoder = new TextEncoder();

  filter.ondata = event => {
	
    var str = decoder.decode(event.data, {stream: true});
   console.log( str.length, str);
    filter.write(encoder.encode(str));
    filter.disconnect();
  }

  return {};
}

browser.webRequest.onBeforeRequest.addListener(
  listener,
  {urls: ["<all_urls>"], types: ["xmlhttprequest"]},
  ["blocking"]
);

console.log( str.length, str); sometimes, the str is fragmentary . for example,like this pic: qq 20180722004155

"alig" it's not the end of response ,but it's end...

the real response is : qq 20180722004826

admimistrator avatar Jul 21 '18 16:07 admimistrator

The example is pretty lousy. This API is streamed, so filter.ondata is called on chunks of the response, not the full response, but if you call filter.disconnect() you stop the process (only printing the first chunk). This article has a bit better examples: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/StreamFilter/ondata.

dyeray avatar Nov 15 '18 17:11 dyeray

@dyeray thanks for that link. Coming here from https://stackoverflow.com/questions/46338823, I couldn't figure out why I was only getting the first chunk of data, and if I removed the disconnect(), the response never made it to its original destination.

seanlinsley avatar Dec 07 '19 19:12 seanlinsley

:heavy_check_mark: added a note to StackOverflow and the top-level MDN docs

seanlinsley avatar Dec 07 '19 19:12 seanlinsley

No activity within the last three years, closing.

rebloor avatar Jul 31 '23 00:07 rebloor