Skip to content

Commit 7f216b4

Browse files
authored
Fix --alwaysAddBehaviorLinks / smart scoping to work as expected: (#1094)
- ensure regular links always obey scope, as using same method - support addLink(url, alwaysObeyScope) from added in, webrecorder/browsertrix-behaviors#150 - scope only ignored if alwaysObeyScope is false and alwaysAddBehaviorLinks is set - move addLink() declaration to setupPage(), as extractLinks() not called for single page scope - simplify, no longer need extra 'callbacks' in PageState
1 parent 77751c8 commit 7f216b4

2 files changed

Lines changed: 23 additions & 34 deletions

File tree

src/crawler.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,25 @@ export class Crawler {
814814
}
815815

816816
async setupPage(opts: WorkerState) {
817-
const { page, cdp, workerid, callbacks, frameIdToExecId, recorder } = opts;
817+
const { page, cdp, workerid, frameIdToExecId, recorder } = opts;
818+
819+
const addLink = async (url: string, alwaysObeyScope: boolean = false) => {
820+
const { seedId, depth, extraHops = 0, logDetails } = opts.data;
821+
822+
// if not always obeying scope and allowed to ignore scope, set to true
823+
const ignoreScope =
824+
!alwaysObeyScope && this.params.alwaysAddBehaviorLinks;
825+
826+
await this.queueInScopeUrls({
827+
seedId,
828+
urls: [url],
829+
depth,
830+
extraHops,
831+
pageUrl: page.url(),
832+
logDetails,
833+
ignoreScope,
834+
});
835+
};
818836

819837
await this.browser.setupPage({ page, cdp });
820838

@@ -865,10 +883,7 @@ export class Crawler {
865883
await this.screencaster.screencastPage(page, cdp, workerid);
866884
}
867885

868-
await page.exposeFunction(
869-
BxFunctionBindings.AddLinkFunc,
870-
(url: string) => callbacks.addLink && callbacks.addLink(url),
871-
);
886+
await page.exposeFunction(BxFunctionBindings.AddLinkFunc, addLink);
872887

873888
// used for both behaviors and link extraction now
874889
await this.browser.addInitScript(page, btrixBehaviors);
@@ -999,7 +1014,7 @@ self.__bx_behaviors.selectMainBehavior();
9991014
BxFunctionBindings.AddToSeenSet,
10001015
(data: string) => {
10011016
if (data && (data.startsWith("https:") || data.startsWith("http:"))) {
1002-
void callbacks.addLink(data);
1017+
void addLink(data);
10031018
}
10041019
return this.crawlState.addToUserSet(data);
10051020
},
@@ -1122,8 +1137,7 @@ self.__bx_behaviors.selectMainBehavior();
11221137
async crawlPage(opts: WorkerState): Promise<void> {
11231138
await this.writeStats();
11241139

1125-
const { page, cdp, data, workerid, callbacks, recorder } = opts;
1126-
data.callbacks = callbacks;
1140+
const { page, cdp, data, workerid, recorder } = opts;
11271141

11281142
const { url, seedId, depth } = data;
11291143

@@ -2713,25 +2727,7 @@ self.__bx_behaviors.selectMainBehavior();
27132727
selectors: ExtractSelector[],
27142728
logDetails: LogDetails,
27152729
) {
2716-
const { seedId, depth, extraHops = 0, filteredFrames, callbacks } = data;
2717-
2718-
callbacks.addLink = async (url: string, ignoreScope = false) => {
2719-
// if crawler arg set, always ignore scope
2720-
// otherwise, may be determined by behavior addLink()
2721-
if (this.params.alwaysAddBehaviorLinks) {
2722-
ignoreScope = true;
2723-
}
2724-
2725-
await this.queueInScopeUrls({
2726-
seedId,
2727-
urls: [url],
2728-
depth,
2729-
extraHops,
2730-
pageUrl: page.url(),
2731-
logDetails,
2732-
ignoreScope,
2733-
});
2734-
};
2730+
const { filteredFrames } = data;
27352731

27362732
const frames = filteredFrames || page.frames();
27372733

src/util/worker.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export type WorkerState = {
1919
page: Page;
2020
cdp: CDPSession;
2121
workerid: WorkerId;
22-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
23-
callbacks: Record<string, Function>;
2422
recorder: Recorder | null;
2523
markPageUsed: () => void;
2624
frameIdToExecId: Map<string, number>;
@@ -41,9 +39,6 @@ export class PageWorker {
4139
page?: Page | null;
4240
cdp?: CDPSession | null;
4341

44-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
45-
callbacks?: Record<string, Function>;
46-
4742
opts?: WorkerState;
4843

4944
// TODO: Fix this the next time the file is edited.
@@ -171,13 +166,11 @@ export class PageWorker {
171166

172167
this.page = page;
173168
this.cdp = cdp;
174-
this.callbacks = {};
175169

176170
this.opts = {
177171
page,
178172
cdp,
179173
workerid,
180-
callbacks: this.callbacks,
181174
recorder: this.recorder,
182175
frameIdToExecId: new Map<string, number>(),
183176
markPageUsed: () => {

0 commit comments

Comments
 (0)