import _ from "lodash"; import { Processor } from "./processor"; export class NonnativeScriptsProcessor extends Processor { private nonnativeScripts: string[] = []; async preProcess(content: string): Promise { return await this.preserveNonnativeScripts(content); } async postProcess(content: string): Promise { return await this.restoreNonnativeScripts(content); } private async preserveNonnativeScripts(content: string): Promise { return _.replace( content, /]*?type=(["'])(?!(text\/javascript|module))[^\1]*?\1[^>]*?>.*?<\/script>/gis, (match: string) => this.storeNonnativeScripts(match), ); } storeNonnativeScripts(value: string) { const index = this.nonnativeScripts.push(value) - 1; return this.getNonnativeScriptPlaceholder(index.toString()); } private async restoreNonnativeScripts(content: string): Promise { return _.replace( content, new RegExp(`${this.getNonnativeScriptPlaceholder("(\\d+)")}`, "gmi"), (_match: any, p1: number) => `${this.nonnativeScripts[p1]}`, ); } private getNonnativeScriptPlaceholder(replace: string) { return _.replace("", "#", replace); } }