Skip to content

Commit

Permalink
addLink: support dynamic links added from behaviors (#69)
Browse files Browse the repository at this point in the history
* addLink: support dynamic links added from behaviors
autoscroll:
- detect if page url (pathname, not hash) changes when scrolling and abort scroll
- call addLink() with new page url
support running main behavior in replay top frame

* remove commented out code
  • Loading branch information
ikreymer authored Feb 29, 2024
1 parent 719eed2 commit b5b358a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 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.2",
"version": "0.5.3",
"main": "index.js",
"author": "Webrecorder Software",
"license": "AGPL-3.0-or-later",
Expand Down
2 changes: 1 addition & 1 deletion src/autoplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Autoplay extends BackgroundBehavior {
const run = true;

if (this.polling) {
return
return;
}

this.polling = true;
Expand Down
13 changes: 12 additions & 1 deletion src/autoscroll.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Behavior } from "./lib/behavior";
import { sleep, waitUnit, xpathNode, isInViewport, waitUntil } from "./lib/utils";
import { sleep, waitUnit, xpathNode, isInViewport, waitUntil, behaviorLog, addLink } from "./lib/utils";
import { type AutoFetcher } from "./autofetcher";


Expand All @@ -11,6 +11,8 @@ export class AutoScroll extends Behavior {
lastScrollPos: number;
samePosCount: number;

origPath: string;

constructor(autofetcher: AutoFetcher) {
super();

Expand All @@ -24,6 +26,8 @@ export class AutoScroll extends Behavior {

this.lastScrollPos = -1;
this.samePosCount = 0;

this.origPath = document.location.pathname;
}

static id = "Autoscroll";
Expand Down Expand Up @@ -113,6 +117,13 @@ export class AutoScroll extends Behavior {
let lastScrollHeight = self.document.scrollingElement.scrollHeight;

while (this.canScrollMore()) {
if (document.location.pathname !== this.origPath) {
behaviorLog("Location Changed, stopping scroll: " +
`${document.location.pathname} != ${this.origPath}`, "info");
addLink(document.location.href);
return;
}

const scrollHeight = self.document.scrollingElement.scrollHeight;

if (scrollHeight > lastScrollHeight) {
Expand Down
7 changes: 2 additions & 5 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) {
if (self.window.top !== self.window && window["__WB_replay_top"] !== self.window) {
return;
}

Expand Down Expand Up @@ -136,9 +136,6 @@ export class BehaviorManager {
if (this.mainBehavior) {
this.behaviors.push(this.mainBehavior);

if (this.mainBehavior instanceof Behavior) {

}
if (this.mainBehavior instanceof BehaviorRunner) {
return this.mainBehavior.behaviorProps.id;
}
Expand All @@ -154,7 +151,7 @@ export class BehaviorManager {
}

if (typeof(behaviorClass.id) !== "string") {
behaviorLog(`Behavior class must have a string string "id" property`, "error");
behaviorLog("Behavior class must have a string string \"id\" property", "error");
return;
}

Expand Down
6 changes: 6 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export function behaviorLog(data, type = "debug") {
}
}

export function addLink(url) {
if (self["__bx_addLink"]) {
self["__bx_addLink"](url);
}
}

export async function openWindow(url) {
if (self["__bx_open"]) {
const p = new Promise((resolve) => self["__bx_openResolve"] = resolve);
Expand Down

0 comments on commit b5b358a

Please sign in to comment.