Skip to content

Commit 5874960

Browse files
authored
Move custom behaviors behind checkbox (#2545)
WIP for #2541 ## Changes - Moves custom behaviors table to behind "Use Custom Behaviors" checkbox. - Updates autoclick selector to match checkbox reveal layout. - Adds minimum viable user guide documentation of custom behaviors.
1 parent ba57b85 commit 5874960

File tree

4 files changed

+109
-105
lines changed

4 files changed

+109
-105
lines changed

frontend/docs/docs/user-guide/workflow-setup.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ The crawl will be gracefully stopped after reaching this set size in GB.
166166

167167
Customize how and when the browser performs specific operations on a page.
168168

169-
**Built-in Behaviors**
169+
_**Behaviors**_
170170

171171
Behaviors are browser operations that can be enabled for additional page interactivity.
172172

@@ -187,13 +187,17 @@ When clicking a link-like element that would normally result in navigation, auto
187187

188188
- Websites that use `<a>` in place of a `<button>` to reveal in-page content.
189189

190-
### Click Selector
190+
#### Click Selector
191191

192192
When autoclick is enabled, you can customize which element is automatically clicked by specifying a CSS selector.
193193

194194
See [Basic CSS selectors (MDN)](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Basic_selectors) for examples of valid CSS selectors.
195195

196-
**Page Timing**
196+
### Use Custom Behaviors
197+
198+
Enable custom behaviors to add your own behavior scripts. See [webrecorder/browsertrix-behaviors](https://github.com/webrecorder/browsertrix-behaviors) for more information.
199+
200+
_**Page Timing**_
197201

198202
Page timing gives you more granular control over how long the browser should stay on a page and when behaviors should run on a page. Add limits to decrease the amount of time the browser spends on a page, and add delays to increase the amount of time the browser waits on a page. Adding delays will increase the total amount of time spent on a crawl and may impact your overall crawl minutes.
199203

frontend/src/features/crawl-workflows/workflow-editor.ts

+96-101
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ https://archiveweb.page/images/${"logo.svg"}`}
12671267
`;
12681268
}
12691269

1270-
private renderBehaviors() {
1270+
private renderPageBehavior() {
12711271
return html`
12721272
${this.renderSectionHeading(labelFor.behaviors)}
12731273
${inputCol(
@@ -1284,77 +1284,65 @@ https://archiveweb.page/images/${"logo.svg"}`}
12841284
)}
12851285
${inputCol(
12861286
html`<sl-checkbox
1287-
name="autoclickBehavior"
1288-
?checked=${this.formState.autoclickBehavior}
1289-
>
1290-
${labelFor.autoclickBehavior}
1291-
</sl-checkbox>`,
1287+
name="autoclickBehavior"
1288+
?checked=${this.formState.autoclickBehavior}
1289+
>
1290+
${labelFor.autoclickBehavior}
1291+
</sl-checkbox>
1292+
1293+
${when(
1294+
this.formState.autoclickBehavior,
1295+
() =>
1296+
html`<div class="mt-3">
1297+
<btrix-syntax-input
1298+
name="clickSelector"
1299+
label=${labelFor.clickSelector}
1300+
language="css"
1301+
value=${this.formState.clickSelector}
1302+
placeholder="${msg("Default:")} ${DEFAULT_AUTOCLICK_SELECTOR}"
1303+
disableTooltip
1304+
@btrix-change=${(
1305+
e: BtrixChangeEvent<typeof this.formState.clickSelector>,
1306+
) => {
1307+
const el = e.target as SyntaxInput;
1308+
const value = e.detail.value.trim();
1309+
1310+
if (value) {
1311+
try {
1312+
// Validate selector
1313+
this.cssParser(value);
1314+
1315+
this.updateFormState(
1316+
{
1317+
clickSelector: e.detail.value,
1318+
},
1319+
true,
1320+
);
1321+
} catch {
1322+
el.setCustomValidity(
1323+
msg("Please enter a valid CSS selector"),
1324+
);
1325+
}
1326+
}
1327+
}}
1328+
></btrix-syntax-input>
1329+
</div> `,
1330+
)} `,
12921331
)}
12931332
${this.renderHelpTextCol(
1294-
msg(
1295-
`Automatically click on all link-like elements without navigating away from the page.`,
1296-
),
1297-
false,
1298-
)}
1299-
${when(
1300-
this.formState.autoclickBehavior,
1301-
() => html`
1302-
${inputCol(
1303-
html`<btrix-syntax-input
1304-
name="clickSelector"
1305-
label=${labelFor.clickSelector}
1306-
language="css"
1307-
value=${this.formState.clickSelector}
1308-
placeholder="${msg("Default:")} ${DEFAULT_AUTOCLICK_SELECTOR}"
1309-
disableTooltip
1310-
@btrix-change=${(
1311-
e: BtrixChangeEvent<typeof this.formState.clickSelector>,
1312-
) => {
1313-
const el = e.target as SyntaxInput;
1314-
const value = e.detail.value.trim();
1315-
1316-
if (value) {
1317-
try {
1318-
// Validate selector
1319-
this.cssParser(value);
1320-
1321-
this.updateFormState(
1322-
{
1323-
clickSelector: e.detail.value,
1324-
},
1325-
true,
1326-
);
1327-
1328-
this.clickSelector?.removeAttribute("data-invalid");
1329-
this.clickSelector?.removeAttribute("data-user-invalid");
1330-
} catch {
1331-
el.setCustomValidity(
1332-
msg("Please enter a valid CSS selector"),
1333-
);
1334-
}
1335-
}
1336-
}}
1337-
@btrix-invalid=${() => {
1338-
/**
1339-
* HACK Set data attribute manually so that
1340-
* table works with `syncTabErrorState`
1341-
*
1342-
* FIXME Should be fixed with
1343-
* https://github.com/webrecorder/browsertrix/issues/2497
1344-
* or
1345-
* https://github.com/webrecorder/browsertrix/issues/2536
1346-
*/
1347-
this.clickSelector?.setAttribute("data-invalid", "true");
1348-
this.clickSelector?.setAttribute("data-user-invalid", "true");
1349-
}}
1350-
></btrix-syntax-input>`,
1333+
html`
1334+
${msg(
1335+
`Automatically click on all link-like elements without navigating away from the page.`,
13511336
)}
1352-
${this.renderHelpTextCol(
1353-
html`${msg(
1354-
`Customize the CSS selector used to autoclick elements.`,
1355-
)} <span class="sr-only">${msg('Defaults to "a".')}</span>`,
1337+
${when(
1338+
this.formState.autoclickBehavior,
1339+
() =>
1340+
html`<br /><br />${msg(
1341+
`Optionally, specify the CSS selector used to autoclick elements.`,
1342+
)} <span class="sr-only">${msg('Defaults to "a".')}</span>`,
13561343
)}
13571344
`,
1345+
false,
13581346
)}
13591347
${this.renderCustomBehaviors()}
13601348
${this.renderSectionHeading(msg("Page Timing"))}
@@ -1421,32 +1409,29 @@ https://archiveweb.page/images/${"logo.svg"}`}
14211409

14221410
private renderCustomBehaviors() {
14231411
return html`
1424-
${this.renderSectionHeading(labelFor.customBehaviors)}
14251412
${inputCol(
1426-
html`<btrix-custom-behaviors-table
1427-
.customBehaviors=${this.initialWorkflow?.config.customBehaviors || []}
1428-
editable
1429-
@btrix-change=${() => {
1430-
this.customBehaviorsTable?.removeAttribute("data-invalid");
1431-
this.customBehaviorsTable?.removeAttribute("data-user-invalid");
1432-
}}
1433-
@btrix-invalid=${() => {
1434-
/**
1435-
* HACK Set data attribute manually so that
1436-
* table works with `syncTabErrorState`
1437-
*
1438-
* FIXME Should be fixed with
1439-
* https://github.com/webrecorder/browsertrix/issues/2497
1440-
* or
1441-
* https://github.com/webrecorder/browsertrix/issues/2536
1442-
*/
1443-
this.customBehaviorsTable?.setAttribute("data-invalid", "true");
1444-
this.customBehaviorsTable?.setAttribute(
1445-
"data-user-invalid",
1446-
"true",
1447-
);
1448-
}}
1449-
></btrix-custom-behaviors-table>`,
1413+
html`<sl-checkbox
1414+
?checked=${this.formState.customBehavior}
1415+
@sl-change=${() =>
1416+
this.updateFormState({
1417+
customBehavior: !this.formState.customBehavior,
1418+
})}
1419+
>
1420+
${msg("Use Custom Behaviors")}
1421+
</sl-checkbox>
1422+
1423+
${when(
1424+
this.formState.customBehavior,
1425+
() => html`
1426+
<div class="mt-3">
1427+
<btrix-custom-behaviors-table
1428+
.customBehaviors=${this.initialWorkflow?.config
1429+
.customBehaviors || []}
1430+
editable
1431+
></btrix-custom-behaviors-table>
1432+
</div>
1433+
`,
1434+
)} `,
14501435
)}
14511436
${this.renderHelpTextCol(
14521437
msg(
@@ -1809,7 +1794,7 @@ https://archiveweb.page/images/${"logo.svg"}`}
18091794
{
18101795
name: "behaviors",
18111796
desc: msg("Customize how the browser loads and interacts with a page."),
1812-
render: this.renderBehaviors,
1797+
render: this.renderPageBehavior,
18131798
},
18141799
{
18151800
name: "browserSettings",
@@ -2208,20 +2193,30 @@ https://archiveweb.page/images/${"logo.svg"}`}
22082193
// See https://github.com/webrecorder/browsertrix/issues/2536
22092194
if (
22102195
this.formState.autoclickBehavior &&
2211-
!this.clickSelector?.checkValidity()
2196+
this.clickSelector
22122197
) {
2213-
this.clickSelector?.reportValidity();
2214-
return;
2198+
if (!this.clickSelector.checkValidity()) {
2199+
this.clickSelector.reportValidity();
2200+
return;
2201+
}
22152202
}
22162203

22172204
// Wait for custom behaviors validation to finish
22182205
// TODO Move away from manual validation check
22192206
// See https://github.com/webrecorder/browsertrix/issues/2536
2220-
try {
2221-
await this.customBehaviorsTable?.taskComplete;
2222-
} catch {
2223-
this.customBehaviorsTable?.reportValidity();
2224-
return;
2207+
2208+
if (this.formState.customBehavior && this.customBehaviorsTable) {
2209+
if (!this.customBehaviorsTable.checkValidity()) {
2210+
this.customBehaviorsTable.reportValidity();
2211+
return;
2212+
}
2213+
2214+
try {
2215+
await this.customBehaviorsTable.taskComplete;
2216+
} catch {
2217+
this.customBehaviorsTable.reportValidity();
2218+
return;
2219+
}
22252220
}
22262221

22272222
const isValid = await this.checkFormValidity(this.formElem);

frontend/src/strings/crawl-workflows/labels.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { msg } from "@lit/localize";
22

33
export const labelFor = {
4-
behaviors: msg("Built-in Behaviors"),
4+
behaviors: msg("Behaviors"),
55
customBehaviors: msg("Custom Behaviors"),
66
autoscrollBehavior: msg("Autoscroll"),
77
autoclickBehavior: msg("Autoclick"),

frontend/src/utils/workflow.ts

+5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export type FormState = {
8989
description: WorkflowParams["description"];
9090
autoscrollBehavior: boolean;
9191
autoclickBehavior: boolean;
92+
customBehavior: boolean;
9293
userAgent: string | null;
9394
crawlerChannel: string;
9495
proxyId: string | null;
@@ -151,6 +152,7 @@ export const getDefaultFormState = (): FormState => ({
151152
proxyId: null,
152153
selectLinks: DEFAULT_SELECT_LINKS,
153154
clickSelector: DEFAULT_AUTOCLICK_SELECTOR,
155+
customBehavior: false,
154156
});
155157

156158
export const mapSeedToUrl = (arr: Seed[]) =>
@@ -296,6 +298,9 @@ export function getInitialFormState(params: {
296298
autoclickBehavior: params.initialWorkflow.config.behaviors
297299
? params.initialWorkflow.config.behaviors.includes(Behavior.AutoClick)
298300
: defaultFormState.autoclickBehavior,
301+
customBehavior: Boolean(
302+
params.initialWorkflow.config.customBehaviors.length,
303+
),
299304
selectLinks: params.initialWorkflow.config.selectLinks,
300305
clickSelector: params.initialWorkflow.config.clickSelector,
301306
userAgent:

0 commit comments

Comments
 (0)