Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
WebExtension : using webrequest to push my content for matching url, is webRequest.StreamFilter.onstart the way to go?

I want to write a webextension that pushes fake json content when accessing any url that matches a pattern.
I started with the MSDN doc.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/StreamFilter/ondata

code is rather straightforward

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();
  }
}

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

But this is not what.
I don't want to (wait for then) alter content. I want to push my content right away.

So I changed the listener for

  filter.onstart = event => {
    var url = event. ???
    if (url.indexOf("any string I'd like to look for")) 
    {
      filter.write(encoder.encode("some text to display"));
      filter.close(); 
    }
    else // I'm not interested in you, just process as usual
    {
      filter.disconnect()
    }
  }

My aim is to catch any url, look for a pattern in it and display my content if it matches.
Is looking for onstart the right way to do it?

  • If yes, I found strange than googling does not return more exemples and I can't find doc for the event that occurs for the on start.
    How is this event called? Where can I find doc for it (and events that occured ondata onclose...)?

  • If no,
    what will be the right way for a web extension to intercept a page request to push (or not) its content instead.

Will my idea of page substitution work if I'm using Ajax/fetch to get it?
Will I be able to set the mimetype too? (I want to push json content)

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!