mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
50 lines
1.0 KiB
TypeScript
50 lines
1.0 KiB
TypeScript
|
import {source as injectedSource} from '../generated/injected.js';
|
||
|
|
||
|
class ScriptInjector {
|
||
|
#updated = false;
|
||
|
#amendments = new Set<string>();
|
||
|
|
||
|
// Appends a statement of the form `(PuppeteerUtil) => {...}`.
|
||
|
append(statement: string): void {
|
||
|
this.#update(() => {
|
||
|
this.#amendments.add(statement);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
pop(statement: string): void {
|
||
|
this.#update(() => {
|
||
|
this.#amendments.delete(statement);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
inject(inject: (script: string) => void, force = false) {
|
||
|
if (this.#updated || force) {
|
||
|
inject(this.#get());
|
||
|
}
|
||
|
this.#updated = false;
|
||
|
}
|
||
|
|
||
|
#update(callback: () => void): void {
|
||
|
callback();
|
||
|
this.#updated = true;
|
||
|
}
|
||
|
|
||
|
#get(): string {
|
||
|
return `(() => {
|
||
|
const module = {};
|
||
|
${injectedSource}
|
||
|
${[...this.#amendments]
|
||
|
.map(statement => {
|
||
|
return `(${statement})(module.exports.default);`;
|
||
|
})
|
||
|
.join('')}
|
||
|
return module.exports.default;
|
||
|
})()`;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @internal
|
||
|
*/
|
||
|
export const scriptInjector = new ScriptInjector();
|