How to use @cliqz/adblocker - 10 common examples

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
// because we do not support this encoding.
      filter.write(event.data);
      filter.disconnect();
      return;
    }

    // Try to guess encoding on first chunk received. The assumption is that
    // we will find either `charset` or `http-equiv` meta-tag in the header.
    // If none of this is found, then we fallback to using `isUTF8` which
    // will make sure `event.data` is valid utf-8 (this check is more
    // costly).
    if (utf8 === undefined) {
      utf8 =
        CHARSET_TAG_RE.test(decoded) ||
        CHARSET_HTTP_EQUIV_RE.test(decoded) ||
        isUTF8(new Uint8Array(event.data));
    }

    // We only proceed to filter this chunk if we could confirm that encoding
    // if utf-8. Otherwise we simply proxy the data as-is to not risk
    // breaking the page.
    if (utf8 === true) {
      filter.write(encoder.encode(htmlFilter.write(decoded)));
    } else {
      filter.write(event.data);
    }
  };
github cliqz-oss / adblocker / bench / micro.ts View on Github external
export function benchRequestParsing({
  requests,
}: {
  requests: Array<{ url: string; frameUrl: string; cpt: string }>;
}) {
  for (let i = 0; i < requests.length; i += 1) {
    const { url, frameUrl, cpt } = requests[i];
    Request.fromRawDetails({
      sourceUrl: frameUrl,
      // @ts-ignore
      type: cpt,
      url,
    });
  }
}
github dot-browser / desktop / src / main / services / web-request.ts View on Github external
async (details: Electron.OnBeforeRequestListenerDetails, callback: any) => {
      const tabId = getTabByWebContentsId(window, details.webContentsId);

      if (engine) {
        const { match, redirect } = engine.match(
          makeRequest({ type: details.resourceType, url: details.url }, parse),
        );

        if (match || redirect) {

          // if(file.get("adblockEnabled") == true) {
            appWindow.webContents.send(`blocked-ad-${tabId}`);

            if (redirect) {
              callback({ redirectURL: redirect });
            } else {
              callback({ cancel: true });
            }
  
            return;
          // }
        }
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;
    }
github dot-browser / desktop / src / main / services / web-request.ts View on Github external
readFile(resolve(path), (err, buffer) => {
      if (err) return console.error(err);

      engine = FiltersEngine.deserialize(buffer);

      /*const { networkFilters, cosmeticFilters } = parseFilters(
        data,
        engine.config,
      );

      engine.update({
        newNetworkFilters: networkFilters,
        newCosmeticFilters: cosmeticFilters,
      });*/
    });
  }
github wexond / desktop / src / main / services / web-request.ts View on Github external
readFile(resolve(path), (err, buffer) => {
      if (err) return console.error(err);

      try {
        engine = FiltersEngine.deserialize(buffer);
      } catch (e) {
        downloadFilters();
      }

      /*const { networkFilters, cosmeticFilters } = parseFilters(
        data,
        engine.config,
      );

      engine.update({
        newNetworkFilters: networkFilters,
        newCosmeticFilters: cosmeticFilters,
      });*/
    });
  } else {
github wexond / desktop / src / main / services / web-request.ts View on Github external
Axios.all(ops).then(res => {
      let data = '';

      for (const res1 of res) {
        data += res1.data;
      }

      engine = FiltersEngine.parse(data);

      writeFile(path, engine.serialize(), err => {
        if (err) return console.error(err);
      });
    });
  };
github cliqz-oss / adblocker / packages / adblocker-electron / adblocker.ts View on Github external
export function fromElectronDetails({
  id,
  url,
  resourceType,
  referrer,
  webContentsId,
}: Electron.OnHeadersReceivedListenerDetails | Electron.OnBeforeRequestListenerDetails): Request {
  return Request.fromRawDetails({
    requestId: `${id}`,
    sourceUrl: referrer,
    tabId: webContentsId,
    type: (resourceType || 'other') as ElectronRequestType,
    url,
  });
}
github cliqz-oss / adblocker / packages / adblocker-puppeteer / adblocker.ts View on Github external
export function fromPuppeteerDetails(details: puppeteer.Request): Request {
  const frame = details.frame();
  const sourceUrl = frame !== null ? frame.url() : undefined;
  return Request.fromRawDetails({
    requestId: `${details.resourceType()} ${details.url()} ${sourceUrl}`,
    sourceUrl,
    type: details.resourceType(),
    url: details.url(),
  });
}
github cliqz-oss / adblocker / packages / adblocker-webextension / adblocker.ts View on Github external
export function fromWebRequestDetails(
  details: WebRequestBeforeRequestDetails | WebRequestHeadersReceivedDetails,
): Request {
  return Request.fromRawDetails({
    requestId: details.requestId,
    sourceUrl: details.initiator || details.originUrl || details.documentUrl,
    tabId: details.tabId,
    type: details.type,
    url: details.url,
  });
}