Skip to content

Commit

Permalink
introduces support for custom page load wait via 'awaitPageLoad(ctx)'…
Browse files Browse the repository at this point in the history
… function.

- if implemented, this function, allows behavior to add custom wait to the page load
- implement awaitPageLoad for instagram behavior, ensure 'main' element is loaded on the page
  • Loading branch information
ikreymer committed Apr 18, 2024
1 parent b5b358a commit baebeae
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/behaviors.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "browsertrix-behaviors",
"version": "0.5.3",
"version": "0.5.4",
"main": "index.js",
"author": "Webrecorder Software",
"license": "AGPL-3.0-or-later",
Expand Down
20 changes: 16 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class BehaviorManager {
this.behaviors.push(new Autoplay(this.autofetch, opts.startEarly));
}

if (self.window.top !== self.window && window["__WB_replay_top"] !== self.window) {
if (!this.isInTopFrame()) {
return;
}

Expand All @@ -105,6 +105,9 @@ export class BehaviorManager {
}

selectMainBehavior() {
if (this.mainBehavior) {
return;
}
const opts = this.opts;
let siteMatch = false;

Expand Down Expand Up @@ -179,6 +182,13 @@ export class BehaviorManager {
}
}

async awaitPageLoad() {
this.selectMainBehavior();
if (this.mainBehavior?.awaitPageLoad) {
await this.mainBehavior.awaitPageLoad();
}
}

async run(opts: BehaviorManagerOpts = DEFAULT_OPTS, restart = false) {
if (restart) {
this.started = false;
Expand All @@ -190,9 +200,7 @@ export class BehaviorManager {
}

this.init(opts, restart);
if (!this.mainBehavior) {
this.selectMainBehavior();
}
this.selectMainBehavior();

await awaitLoad();

Expand Down Expand Up @@ -250,6 +258,10 @@ export class BehaviorManager {
behaviorLog("Queueing Async Fetch Url: " + url);
return this.autofetch.queueUrl(url);
}

isInTopFrame() {
return self.window.top === self.window || window["__WB_replay_top"] === self.window;
}
}


Expand Down
14 changes: 13 additions & 1 deletion src/lib/behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export class Behavior extends BackgroundBehavior {

}

async awaitPageLoad() {
// wait for initial page load here
}

static load() {
if (self["__bx_behaviors"]) {
self["__bx_behaviors"].load(this);
Expand All @@ -102,11 +106,13 @@ export class Behavior extends BackgroundBehavior {
}
}

// WIP: BehaviorRunner class allows for arbitrary behavirs outside of the
// WIP: BehaviorRunner class allows for arbitrary behaviors outside of the
// library to be run through the BehaviorManager

abstract class AbstractBehaviorInst {
abstract run: (ctx: any) => AsyncIterable<any>;

abstract awaitPageLoad?: (ctx: any) => Promise<void>;
}

interface StaticAbstractBehavior {
Expand Down Expand Up @@ -193,6 +199,12 @@ export class BehaviorRunner extends BackgroundBehavior {

}

async awaitPageLoad() {
if (this.inst.awaitPageLoad) {
await this.inst.awaitPageLoad(this.ctx);
}
}

static load() {
if (self["__bx_behaviors"]) {
self["__bx_behaviors"].load(this);
Expand Down
10 changes: 10 additions & 0 deletions src/site/instagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Q = {
commentRoot: "//article[@role='presentation']/div[1]/div[2]//ul",
viewReplies: "//li//button[span[not(count(*)) and text()!='$1']]",
loadMore: "//button[span[@aria-label]]",
pageLoadWaitUntil: "//main"
};

export class InstagramPostsBehavior {
Expand Down Expand Up @@ -246,4 +247,13 @@ export class InstagramPostsBehavior {
await sleep(waitUnit * 5);
}
}

async awaitPageLoad(ctx: any) {
const { Lib, log } = ctx;
const { waitUntilNode } = Lib;

log("Waiting for Instagram to fully load", "info");

await waitUntilNode(Q.pageLoadWaitUntil, document, null, 30000);
}
}

0 comments on commit baebeae

Please sign in to comment.