This repository has been archived on 2023-12-17. You can view files and clone it, but cannot push or open issues or pull requests.
extension/src/contentFilterRegistry.ts

38 lines
990 B
TypeScript
Raw Normal View History

import ContentFilterBase from "./contentFilterBase.js";
export default class ContentFilterRegistry {
private static contentFilters: ContentFilterBase[] = [];
public static hasFilters(type: string): boolean {
return (
ContentFilterRegistry.contentFilters.filter((item) =>
item.getMimeTypes().includes(type)
).length > 0
);
}
public static filter(type: string) {
return async (data: Uint8Array) => {
const filters = ContentFilterRegistry.contentFilters.filter((item) =>
item.getMimeTypes().includes(type)
);
if (!filters.length) {
return data;
}
let filterData = new TextDecoder().decode(data);
for (const filter of filters) {
filterData = await filter.filter(filterData);
}
return new TextEncoder().encode(filterData);
};
}
public static registerFilter(contentFilter: ContentFilterBase): void {
ContentFilterRegistry.contentFilters.push(contentFilter);
}
}