Skip to content

Commit 336ef10

Browse files
ikreymertw4l
andauthored
Add logged in content check for siteSpecific behaviors! (#100)
- Add logged in validation to site-specific behaviors in awaitPageLoad() - use new exposed function, __bx_contentCheckFailed , if it exists, otherwise do nothing. - fixes #99 - add logged-in content assertion for facebook, instagram, twitter, telegram. Add 'is video playing' content assertion - for youtube - bump to 0.9.0 --------- Co-authored-by: Tessa Walsh <tessa@bitarchivist.net>
1 parent 26fef18 commit 336ef10

File tree

11 files changed

+77
-15
lines changed

11 files changed

+77
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "browsertrix-behaviors",
3-
"version": "0.8.5",
3+
"version": "0.9.0",
44
"main": "index.js",
55
"author": "Webrecorder Software",
66
"license": "AGPL-3.0-or-later",

src/autoscroll.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class AutoScroll extends Behavior {
4040
hasScrollEL(obj) {
4141
try {
4242
return !!self["getEventListeners"](obj).scroll;
43-
} catch (e) {
43+
} catch (_) {
4444
// unknown, assume has listeners
4545
this.debug("getEventListeners() not available");
4646
return true;

src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { Autoplay } from "./autoplay";
33
import { AutoScroll } from "./autoscroll";
44
import { AutoClick } from "./autoclick";
55
import { awaitLoad, sleep, behaviorLog, _setLogFunc, _setBehaviorManager, installBehaviors, addLink, checkToJsonOverride } from "./lib/utils";
6-
import { Behavior, BehaviorRunner } from "./lib/behavior";
6+
import { type Behavior, BehaviorRunner } from "./lib/behavior";
7+
import * as Lib from "./lib/utils";
8+
79

810
import siteBehaviors from "./site";
911

@@ -211,7 +213,9 @@ export class BehaviorManager {
211213
this.selectMainBehavior();
212214
if (this.mainBehavior?.awaitPageLoad) {
213215
behaviorLog("Waiting for custom page load via behavior");
214-
await this.mainBehavior.awaitPageLoad();
216+
await this.mainBehavior.awaitPageLoad({Lib});
217+
} else {
218+
behaviorLog("No custom wait behavior");
215219
}
216220
}
217221

src/lib/behavior.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class Behavior extends BackgroundBehavior {
9191

9292
}
9393

94-
async awaitPageLoad() {
94+
async awaitPageLoad(_: any) {
9595
// wait for initial page load here
9696
}
9797

src/lib/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ export async function nextFlowStep(id: number) : Promise<any> {
155155
return {done: true, msg: ""};
156156
}
157157

158+
export function assertContentValid(assertFunc: () => boolean, reason = "invalid") {
159+
if (typeof(self["__bx_contentCheckFailed"]) === "function") {
160+
if (!assertFunc()) {
161+
behaviorLog("Behavior content check failed: " + reason, "error");
162+
callBinding(self["__bx_contentCheckFailed"], reason);
163+
}
164+
}
165+
}
166+
167+
158168
export async function openWindow(url) {
159169
if (self["__bx_open"]) {
160170
const p = new Promise((resolve) => self["__bx_openResolve"] = resolve);

src/site/facebook.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const Q = {
2222
isPhotoVideoPage: /^.*facebook\.com\/[^/]+\/(photos|videos)\/.+/,
2323
isPhotosPage: /^.*facebook\.com\/[^/]+\/photos\/?($|\?)/,
2424
isVideosPage: /^.*facebook\.com\/[^/]+\/videos\/?($|\?)/,
25+
pageLoadWaitUntil: "//div[@role='main']"
2526
};
2627

2728
export class FacebookTimelineBehavior {
@@ -31,9 +32,8 @@ export class FacebookTimelineBehavior {
3132
static id = "Facebook";
3233

3334
static isMatch() {
34-
// disable for now
35-
return false;
36-
//return !!window.location.href.match(/https:\/\/(www\.)?facebook\.com\//);
35+
// match just for posts for now
36+
return !!window.location.href.match(/https:\/\/(www\.)?facebook\.com\/.*\/posts\//);
3737
}
3838

3939
static init() {
@@ -386,4 +386,15 @@ export class FacebookTimelineBehavior {
386386
ctx.state = { "posts": 0, "comments": 0, "videos": 0 };
387387
yield* this.iterPostFeeds(ctx);
388388
}
389+
390+
async awaitPageLoad(ctx: any) {
391+
const { Lib, log } = ctx;
392+
const { assertContentValid, waitUntilNode } = Lib;
393+
394+
log("Waiting for Facebook to fully load", "info");
395+
396+
await waitUntilNode(Q.pageLoadWaitUntil, document, null, 10000);
397+
398+
assertContentValid(() => !!document.querySelector("div[aria-label*='Account Controls' i]"), "not_logged_in");
399+
}
389400
}

src/site/instagram.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,12 @@ export class InstagramPostsBehavior {
251251

252252
async awaitPageLoad(ctx: any) {
253253
const { Lib, log } = ctx;
254-
const { waitUntilNode } = Lib;
254+
const { assertContentValid, waitUntilNode } = Lib;
255255

256-
log("Waiting for Instagram to fully load", "info");
256+
log("Waiting for Instagram to fully load");
257257

258-
await waitUntilNode(Q.pageLoadWaitUntil, document, null, 30000);
258+
await waitUntilNode(Q.pageLoadWaitUntil, document, null, 10000);
259+
260+
assertContentValid(() => !!document.querySelector("*[aria-label='New post']"), "not_logged_in");
259261
}
260262
}

src/site/tiktok.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,22 @@ const Q = {
55
viewMoreThread: ".//p[starts-with(@data-e2e, 'view-more') and string-length(text()) > 0]",
66
profileVideoList: "//div[starts-with(@data-e2e, 'user-post-item-list')]",
77
profileVideoItem: "div[contains(@class, 'DivItemContainerV2')]",
8-
backButton: "button[contains(@class, 'StyledCloseIconContainer')]"
8+
backButton: "button[contains(@class, 'StyledCloseIconContainer')]",
9+
pageLoadWaitUntil: "//*[@role='dialog']"
910
};
1011

1112
export const BREADTH_ALL = Symbol("BREADTH_ALL");
1213

13-
export class TikTokVideoBehavior {
14+
export class TikTokSharedBehavior {
15+
async awaitPageLoad(ctx: any) {
16+
const { assertContentValid, waitUntilNode } = ctx.Lib;
17+
await waitUntilNode(Q.pageLoadWaitUntil, document, null, 10000);
18+
19+
assertContentValid(() => !!document.querySelector("*[aria-label='Messages']"), "not_logged_in");
20+
}
21+
}
22+
23+
export class TikTokVideoBehavior extends TikTokSharedBehavior {
1424
static id = "TikTokVideo";
1525

1626
static init() {
@@ -61,7 +71,9 @@ export class TikTokVideoBehavior {
6171
}
6272
}
6373

64-
export class TikTokProfileBehavior {
74+
75+
76+
export class TikTokProfileBehavior extends TikTokSharedBehavior {
6577
static id = "TikTokProfile";
6678

6779
static isMatch() {

src/site/twitter.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,10 @@ export class TwitterTimelineBehavior {
282282
async* run(ctx) {
283283
yield* this.iterTimeline(ctx, 0);
284284
}
285+
286+
async awaitPageLoad(ctx: any) {
287+
const { sleep, assertContentValid } = ctx.Lib;
288+
await sleep(5);
289+
assertContentValid(() => !document.documentElement.outerHTML.match(/Log In/i), "not_logged_in");
290+
}
285291
}

src/site/youtube.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { AutoScroll } from "../autoscroll";
2+
3+
export class YoutubeBehavior extends AutoScroll {
4+
override async awaitPageLoad(ctx: any) {
5+
const { sleep, assertContentValid } = ctx.Lib;
6+
await sleep(10);
7+
assertContentValid(() => {
8+
const video = document.querySelector("video");
9+
const paused = video && video.paused;
10+
if (paused) {
11+
return false;
12+
}
13+
return document.documentElement.outerHTML.indexOf("not a bot") === -1;
14+
}, "no_video_playing");
15+
}
16+
}

0 commit comments

Comments
 (0)