Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}