Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

browser.webRequest.filterResponseData ondata error #364

Closed
admimistrator opened this issue Jul 21, 2018 · 4 comments
Closed

browser.webRequest.filterResponseData ondata error #364

admimistrator opened this issue Jul 21, 2018 · 4 comments
Labels
example New or updated example idle Issues and pull requests with no activity for three months.

Comments

@admimistrator
Copy link

admimistrator commented Jul 21, 2018

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

@dyeray
Copy link

dyeray commented Nov 15, 2018

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.

@seanlinsley
Copy link

@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
Copy link

✔️ added a note to StackOverflow and the top-level MDN docs

@github-actions github-actions bot added the idle Issues and pull requests with no activity for three months. label Jan 4, 2023
@rebloor rebloor added the example New or updated example label Jun 24, 2023
@github-actions github-actions bot removed the idle Issues and pull requests with no activity for three months. label Jun 25, 2023
@github-actions github-actions bot added the idle Issues and pull requests with no activity for three months. label Jul 25, 2023
@rebloor
Copy link
Collaborator

rebloor commented Jul 31, 2023

No activity within the last three years, closing.

@rebloor rebloor closed this as completed Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
example New or updated example idle Issues and pull requests with no activity for three months.
Projects
None yet
Development

No branches or pull requests

4 participants