title: WebExtension : using webrequest to push my content for matching url, is webRequest.StreamFilter.onstart the way to go?
body: <p>I want to write a webextension that pushes fake json content when accessing any url that matches a pattern.<br />
  I started with the MSDN doc.<br />
  <a href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/StreamFilter/ondata" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/StreamFilter/ondata</a></p>
  <p>code is rather straightforward</p>
  <pre><code>function listener(details) {
    let filter = browser.webRequest.filterResponseData(details.requestId);
    let decoder = new TextDecoder(&quot;utf-8&quot;);
    let encoder = new TextEncoder();
  
    filter.ondata = event =&gt; {
      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: [&quot;https://example.com/*&quot;], types: [&quot;main_frame&quot;]},
    [&quot;blocking&quot;]
  );
  </code></pre>
  <p>But this is not what.<br />
  I don't want to (wait for then) alter content.
  I want to push my content right away.</p>
  <p>So I changed the listener for</p>
  <pre><code>  filter.onstart = event =&gt; {
      var url = event. ???
      if (url.indexOf(&quot;any string I'd like to look for&quot;)) 
      {
        filter.write(encoder.encode(&quot;some text to display&quot;));
        filter.close(); 
      }
      else // I'm not interested in you, just process as usual
      {
        filter.disconnect()
      }
    }
  </code></pre>
  <p>My aim is to catch any url, look for a pattern in it and display my content if it matches.<br />
  Is looking for onstart the right way to do it?</p>
  <ul>
  <li><p>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.<br />
  <strong>How is this event called? Where can I find doc for it (and events that occured ondata onclose...)?</strong></p>
  </li>
  <li><p>If no,<br />
  <strong>what will be the right way for a web extension to intercept a page request to push (or not) its content instead.</strong></p>
  </li>
  </ul>
  <p>Will my idea of page substitution work if I'm using Ajax/fetch to get it?<br />
  Will I be able to set the mimetype too? (I want to push json content)</p>