browsertrix-crawler/behaviors/bgbehaviors.js
Ilya Kreymer 8c85ca2749 background behaviors refactor: (fixes #23)
- move auto-play, auto-fetch and auto-scroll behaviors to behaviors/global/*
- bgbehaviors manages these background behaviors
- command line --bgbehaviors option specifies which background behaviors to run (defaults to auto-fetch and auto-play)
2021-02-08 22:21:34 -08:00

44 lines
1.1 KiB
JavaScript

const AutoPlayBehavior = require("./global/autoplay");
const AutoFetchBehavior = require("./global/autofetcher");
const AutoScrollBehavior = require("./global/autoscroll");
// ===========================================================================
class BackgroundBehaviors
{
constructor(bgbehaviors) {
this.doAutoFetch = bgbehaviors.includes("auto-fetch");
this.doAutoPlay = bgbehaviors.includes("auto-play");
this.doAutoScroll = bgbehaviors.includes("auto-scroll");
}
async setup(page, crawler) {
const behaviors = [];
try {
if (this.doAutoFetch) {
behaviors.push(new AutoFetchBehavior());
}
if (this.doAutoPlay) {
behaviors.push(new AutoPlayBehavior());
}
if (this.doAutoScroll) {
behaviors.push(new AutoScrollBehavior());
}
await Promise.all(behaviors.map(b => b.beforeLoad(page, crawler)));
} catch (err) {
console.log(err);
}
return () => Promise.all(behaviors.map(b => b.afterLoad(page, crawler)));
}
}
module.exports = BackgroundBehaviors;