2021-06-07 17:43:36 -07:00
|
|
|
const ws = require("ws");
|
|
|
|
const http = require("http");
|
|
|
|
const url = require("url");
|
|
|
|
const fs = require("fs");
|
2021-07-06 20:22:27 -07:00
|
|
|
const path = require("path");
|
2021-06-07 17:43:36 -07:00
|
|
|
|
2022-02-23 12:09:48 -08:00
|
|
|
const { initRedis } = require("./redis");
|
|
|
|
|
|
|
|
|
2021-06-07 17:43:36 -07:00
|
|
|
const SingleBrowserImplementation = require("puppeteer-cluster/dist/concurrency/SingleBrowserImplementation").default;
|
|
|
|
|
2022-02-23 14:39:33 -08:00
|
|
|
const indexHTML = fs.readFileSync(path.join(__dirname, "..", "html", "screencast.html"), {encoding: "utf8"});
|
2021-06-07 17:43:36 -07:00
|
|
|
|
|
|
|
|
|
|
|
// ===========================================================================
|
2022-02-23 12:09:48 -08:00
|
|
|
class WSTransport
|
2021-06-07 17:43:36 -07:00
|
|
|
{
|
2022-02-23 12:09:48 -08:00
|
|
|
constructor(port) {
|
2021-06-07 17:43:36 -07:00
|
|
|
this.allWS = new Set();
|
|
|
|
|
2022-02-23 12:09:48 -08:00
|
|
|
this.caster = null;
|
2021-06-07 17:43:36 -07:00
|
|
|
|
|
|
|
this.wss = new ws.Server({ noServer: true });
|
|
|
|
|
|
|
|
this.wss.on("connection", (ws) => this.initWebSocket(ws));
|
|
|
|
|
2022-02-23 12:09:48 -08:00
|
|
|
this.httpServer = http.createServer((...args) => this.handleRequest(...args));
|
2021-06-07 17:43:36 -07:00
|
|
|
this.httpServer.on("upgrade", (request, socket, head) => {
|
|
|
|
const pathname = url.parse(request.url).pathname;
|
|
|
|
|
|
|
|
if (pathname === "/ws") {
|
|
|
|
this.wss.handleUpgrade(request, socket, head, (ws) => {
|
|
|
|
this.wss.emit("connection", ws, request);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.httpServer.listen(port);
|
|
|
|
}
|
|
|
|
|
2022-02-23 12:09:48 -08:00
|
|
|
async handleRequest(req, res) {
|
|
|
|
const pathname = url.parse(req.url).pathname;
|
|
|
|
switch (pathname) {
|
|
|
|
case "/":
|
|
|
|
res.writeHead(200, {"Content-Type": "text/html"});
|
|
|
|
res.end(indexHTML);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.writeHead(404, {"Content-Type": "text/html"});
|
|
|
|
res.end("Not Found");
|
|
|
|
}
|
|
|
|
|
2021-06-07 17:43:36 -07:00
|
|
|
initWebSocket(ws) {
|
2022-02-23 12:09:48 -08:00
|
|
|
for (const packet of this.caster.iterCachedData()) {
|
|
|
|
ws.send(JSON.stringify(packet));
|
2021-06-07 17:43:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this.allWS.add(ws);
|
|
|
|
|
|
|
|
if (this.allWS.size === 1) {
|
2022-02-23 12:09:48 -08:00
|
|
|
this.caster.startCastAll();
|
2021-06-07 17:43:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ws.on("close", () => {
|
0.4.1 Release! (#70)
* optimization: don't intercept requests if no blockRules set
* page load: set waitUntil to use networkidle2 instead of networkidle0 as reasonable default for most pages
* add --behaviorTimeout to set max running time for behaviors (defaults to 90 seconds)
* refactor profile loadProfile/saveProfile to util/browser.js
- support augmenting existing profile when creating a new profile
* screencasting: convert newContext to window instead of page by default, instead of just warning about it
* shared multiplatform image support:
- determine browser exe from list of options, getBrowserExe() returns current exe
- supports running with 'google-chrome' under amd64, and 'chromium-browser' under arm64
- update to multiplatform oldwebtoday/chrome:91 as browser image
- enable multiplatform build with latest build-push-action@v2
* seeds: add trim() to seed URLs
* logging: reduce initial debug logging, enable only if '--logging debug' is set. log if profile, text-extraction enabled, and post-processing stages automatically
* profile creation: add --windowSize flag, set default to 1600x900, default to loading Application tab, tweak UI styles
* extractLinks: support passing in custom property to get link, and also loading as an attribute via getAttribute. Fixes #25
* update CHANGES and README with new features
* bump version to 0.4.1
2021-07-22 14:24:51 -07:00
|
|
|
//console.log("Screencast WebSocket Disconnected");
|
2021-06-07 17:43:36 -07:00
|
|
|
this.allWS.delete(ws);
|
|
|
|
|
|
|
|
if (this.allWS.size === 0) {
|
2022-02-23 12:09:48 -08:00
|
|
|
this.caster.stopCastAll();
|
2021-06-07 17:43:36 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-23 12:09:48 -08:00
|
|
|
sendAll(packet) {
|
|
|
|
packet = JSON.stringify(packet);
|
2021-06-07 17:43:36 -07:00
|
|
|
for (const ws of this.allWS) {
|
2022-02-23 12:09:48 -08:00
|
|
|
ws.send(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isActive() {
|
|
|
|
return this.allWS.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ===========================================================================
|
|
|
|
class RedisPubSubTransport
|
|
|
|
{
|
|
|
|
constructor(redisUrl, crawlId) {
|
|
|
|
this.numConnections = 0;
|
|
|
|
this.castChannel = `c:${crawlId}:cast`;
|
|
|
|
this.ctrlChannel = `c:${crawlId}:ctrl`;
|
|
|
|
|
|
|
|
this.init(redisUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
async init(redisUrl) {
|
|
|
|
this.redis = await initRedis(redisUrl);
|
|
|
|
|
|
|
|
const subRedis = await initRedis(redisUrl);
|
|
|
|
|
|
|
|
await subRedis.subscribe(this.ctrlChannel);
|
|
|
|
|
2022-03-02 13:26:11 -08:00
|
|
|
subRedis.on("message", async (channel, message) => {
|
2022-02-23 12:09:48 -08:00
|
|
|
if (channel !== this.ctrlChannel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (message) {
|
|
|
|
case "connect":
|
|
|
|
this.numConnections++;
|
|
|
|
if (this.numConnections === 1) {
|
|
|
|
this.caster.startCastAll();
|
|
|
|
} else {
|
|
|
|
for (const packet of this.caster.iterCachedData()) {
|
2022-03-02 13:26:11 -08:00
|
|
|
await this.sendAll(packet);
|
2022-02-23 12:09:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "disconnect":
|
|
|
|
this.numConnections--;
|
|
|
|
if (this.numConnections === 0) {
|
2022-03-02 13:26:11 -08:00
|
|
|
this.caster.stopCastAll();
|
2022-02-23 12:09:48 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-02 13:26:11 -08:00
|
|
|
async sendAll(packet) {
|
|
|
|
await this.redis.publish(this.castChannel, JSON.stringify(packet));
|
2022-02-23 12:09:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async isActive() {
|
|
|
|
const result = await this.redis.pubsub("numsub", this.castChannel);
|
|
|
|
return (result.length > 1 ? result[1] > 0: false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ===========================================================================
|
|
|
|
class ScreenCaster
|
|
|
|
{
|
2022-03-02 13:26:11 -08:00
|
|
|
constructor(transport, numWorkers) {
|
2022-02-23 12:09:48 -08:00
|
|
|
this.transport = transport;
|
|
|
|
this.transport.caster = this;
|
|
|
|
|
|
|
|
this.caches = new Map();
|
|
|
|
this.urls = new Map();
|
|
|
|
|
|
|
|
this.targets = new Map();
|
2022-02-23 14:39:33 -08:00
|
|
|
|
|
|
|
// todo: make customizable
|
|
|
|
this.maxWidth = 640;
|
|
|
|
this.maxHeight = 480;
|
2022-03-02 13:26:11 -08:00
|
|
|
|
|
|
|
this.initMsg = {
|
|
|
|
msg: "init",
|
|
|
|
width: this.maxWidth,
|
|
|
|
height: this.maxHeight,
|
|
|
|
browsers: numWorkers
|
|
|
|
};
|
2022-02-23 12:09:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
*iterCachedData() {
|
2022-03-02 13:26:11 -08:00
|
|
|
yield this.initMsg;
|
2022-02-23 12:09:48 -08:00
|
|
|
const msg = "screencast";
|
|
|
|
for (const id of this.caches.keys()) {
|
|
|
|
const data = this.caches.get(id);
|
|
|
|
const url = this.urls.get(id);
|
|
|
|
yield {msg, id, url, data};
|
2021-06-07 17:43:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 12:09:48 -08:00
|
|
|
|
2021-06-07 17:43:36 -07:00
|
|
|
async newTarget(target) {
|
|
|
|
const cdp = await target.createCDPSession();
|
|
|
|
const id = target._targetId;
|
|
|
|
|
|
|
|
this.targets.set(id, cdp);
|
2022-02-23 12:09:48 -08:00
|
|
|
this.urls.set(id, target.url());
|
2021-06-07 17:43:36 -07:00
|
|
|
|
2022-02-23 12:09:48 -08:00
|
|
|
const msg = "screencast";
|
2021-06-07 17:43:36 -07:00
|
|
|
|
|
|
|
cdp.on("Page.screencastFrame", async (resp) => {
|
|
|
|
const data = resp.data;
|
|
|
|
const sessionId = resp.sessionId;
|
2022-02-23 14:39:33 -08:00
|
|
|
const url = target.url();
|
2021-06-07 17:43:36 -07:00
|
|
|
|
|
|
|
this.caches.set(id, data);
|
2022-02-23 14:39:33 -08:00
|
|
|
this.urls.set(id, url);
|
2022-02-23 12:09:48 -08:00
|
|
|
|
2022-03-02 13:26:11 -08:00
|
|
|
//if (url !== "about:blank") {
|
|
|
|
await this.transport.sendAll({msg, id, data, url});
|
|
|
|
//}
|
2022-02-23 12:09:48 -08:00
|
|
|
|
2021-07-20 15:45:51 -07:00
|
|
|
try {
|
|
|
|
await cdp.send("Page.screencastFrameAck", {sessionId});
|
|
|
|
} catch(e) {
|
0.4.1 Release! (#70)
* optimization: don't intercept requests if no blockRules set
* page load: set waitUntil to use networkidle2 instead of networkidle0 as reasonable default for most pages
* add --behaviorTimeout to set max running time for behaviors (defaults to 90 seconds)
* refactor profile loadProfile/saveProfile to util/browser.js
- support augmenting existing profile when creating a new profile
* screencasting: convert newContext to window instead of page by default, instead of just warning about it
* shared multiplatform image support:
- determine browser exe from list of options, getBrowserExe() returns current exe
- supports running with 'google-chrome' under amd64, and 'chromium-browser' under arm64
- update to multiplatform oldwebtoday/chrome:91 as browser image
- enable multiplatform build with latest build-push-action@v2
* seeds: add trim() to seed URLs
* logging: reduce initial debug logging, enable only if '--logging debug' is set. log if profile, text-extraction enabled, and post-processing stages automatically
* profile creation: add --windowSize flag, set default to 1600x900, default to loading Application tab, tweak UI styles
* extractLinks: support passing in custom property to get link, and also loading as an attribute via getAttribute. Fixes #25
* update CHANGES and README with new features
* bump version to 0.4.1
2021-07-22 14:24:51 -07:00
|
|
|
//console.log("Ack Failed, probably window/tab already closed", e);
|
2021-07-20 15:45:51 -07:00
|
|
|
}
|
2021-06-07 17:43:36 -07:00
|
|
|
});
|
|
|
|
|
2022-02-23 12:09:48 -08:00
|
|
|
if (await this.transport.isActive()) {
|
2021-06-07 17:43:36 -07:00
|
|
|
await this.startCast(cdp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async endTarget(target) {
|
|
|
|
const id = target._targetId;
|
|
|
|
const cdp = this.targets.get(id);
|
|
|
|
if (!cdp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.stopCast(cdp);
|
|
|
|
|
|
|
|
this.caches.delete(id);
|
|
|
|
this.urls.delete(id);
|
|
|
|
|
2022-03-02 13:26:11 -08:00
|
|
|
await this.transport.sendAll({msg: "close", id});
|
2022-02-23 12:09:48 -08:00
|
|
|
|
|
|
|
this.targets.delete(id);
|
|
|
|
|
2021-07-20 15:45:51 -07:00
|
|
|
try {
|
|
|
|
await cdp.detach();
|
|
|
|
} catch (e) {
|
|
|
|
// already detached
|
|
|
|
}
|
2021-06-07 17:43:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async startCast(cdp) {
|
|
|
|
if (cdp._startedCast) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cdp._startedCast = true;
|
|
|
|
|
2022-03-02 13:26:11 -08:00
|
|
|
await cdp.send("Page.startScreencast", {format: "png", everyNthFrame: 2, maxWidth: this.maxWidth, maxHeight: this.maxHeight});
|
2021-06-07 17:43:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async stopCast(cdp) {
|
|
|
|
if (!cdp._startedCast) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cdp._startedCast = false;
|
2021-07-20 15:45:51 -07:00
|
|
|
try {
|
|
|
|
await cdp.send("Page.stopScreencast");
|
|
|
|
} catch (e) {
|
|
|
|
// likely already stopped
|
|
|
|
}
|
2021-06-07 17:43:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
startCastAll() {
|
|
|
|
const promises = [];
|
|
|
|
|
|
|
|
for (const cdp of this.targets.values()) {
|
|
|
|
promises.push(this.startCast(cdp));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.allSettled(promises);
|
|
|
|
}
|
|
|
|
|
|
|
|
stopCastAll() {
|
|
|
|
const promises = [];
|
|
|
|
|
|
|
|
for (const cdp of this.targets.values()) {
|
|
|
|
promises.push(this.stopCast(cdp));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.allSettled(promises);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ===========================================================================
|
|
|
|
class NewWindowPage extends SingleBrowserImplementation {
|
|
|
|
async init() {
|
|
|
|
await super.init();
|
|
|
|
|
|
|
|
this.newTargets = [];
|
|
|
|
|
|
|
|
this.nextPromise();
|
|
|
|
|
|
|
|
this.mainPage = await this.browser.newPage();
|
|
|
|
|
|
|
|
this.pages = [];
|
|
|
|
this.reuse = true;
|
|
|
|
|
|
|
|
await this.mainPage.goto("about:blank");
|
|
|
|
|
|
|
|
this.mainTarget = this.mainPage.target();
|
|
|
|
|
|
|
|
this.browser.on("targetcreated", (target) => {
|
|
|
|
if (this._nextTarget && target.opener() === this.mainTarget) {
|
|
|
|
this.newTargets.push(target);
|
|
|
|
this._nextTarget();
|
|
|
|
this.nextPromise();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
nextPromise() {
|
|
|
|
this._nextPromise = new Promise((resolve) => this._nextTarget = resolve);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getNewPage() {
|
|
|
|
const p = this._nextPromise;
|
|
|
|
|
|
|
|
await this.mainPage.evaluate("window.open('about:blank', '', 'resizable');");
|
|
|
|
|
|
|
|
await p;
|
|
|
|
|
|
|
|
const target = this.newTargets.shift();
|
|
|
|
|
|
|
|
return {page: await target.page() };
|
|
|
|
}
|
|
|
|
|
|
|
|
async createResources() {
|
|
|
|
if (this.pages.length) {
|
|
|
|
return {page: this.pages.shift()};
|
|
|
|
}
|
|
|
|
return await this.getNewPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
async freeResources(resources) {
|
|
|
|
if (this.reuse) {
|
|
|
|
this.pages.push(resources.page);
|
|
|
|
} else {
|
|
|
|
await resources.page.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-02-23 12:09:48 -08:00
|
|
|
module.exports = { ScreenCaster, NewWindowPage, WSTransport, RedisPubSubTransport };
|