How to use the @cliqz/adblocker.StreamingHtmlFilter function in @cliqz/adblocker

To help you get started, we’ve selected a few @cliqz/adblocker examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cliqz-oss / adblocker / packages / adblocker-webextension / adblocker.ts View on Github external
export function filterRequestHTML({ id }: { id: string }, rules: HTMLSelector[]): void {
  // @ts-ignore
  const browser: any = typeof browser !== 'undefined' ? browser : chrome;

  // Create filter to observe loading of resource
  const filter: StreamFilter = browser.webRequest.filterResponseData(id);
  const decoder = new TextDecoder();
  const encoder = new TextEncoder();
  const htmlFilter = new StreamingHtmlFilter(rules);
  let utf8: undefined | boolean;

  filter.ondata = (event) => {
    let decoded = '';
    try {
      // Attempt decoding chunk (ArrayBuffer) into a string.
      decoded = decoder.decode(event.data, { stream: true });
    } catch (ex) {
      // If we fail to decode chunk, then we need to be extra conservative
      // and we stop listening to streaming response. This is most likely
      // because we do not support this encoding.
      filter.write(event.data);
      filter.disconnect();
      return;
    }