Skip to content

Commit e6f250d

Browse files
committed
behaviors: add loop control selector
Right now, our loop config in behaviours is purely based on the loop limit. We can be smarter than that, though - sometimes we can know when it's safe to *stop* clicking the same element over and over by running another selector. A good example of this is page pagination. Most sites with JavaScript-based pagination will also disable the "next" button once the browser has reached the last page of results. Using that, we can configure a behaviour that looks something like this: ```yaml - url_regex: '^.*$' behavior_js_template: umbraBehavior.js.j2 request_idle_timeout_sec: 10 default_parameters: interval: 10 actions: - selector: .next repeatSameElement: true repeatUntilSelector: .next[disabled] # the repeatUntil selector makes long loops safe # since we'll break out of the loop once we hit the # disabled button! limit: 1000 ```
1 parent 47f5c06 commit e6f250d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

brozzler/js-templates/umbraBehavior.js.j2

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class UmbraBehavior {
3535
var selector = this.actions[k].selector;
3636
var childSelector = this.actions[k].childSelector;
3737
var repeatSameElement = this.actions[k].repeatSameElement ? this.actions[k].repeatSameElement : false;
38+
var repeatUntilSelector = this.actions[k].repeatUntilSelector ? this.actions[k].repeatUntilSelector : null;
3839
var limit = this.actions[k].limit ? this.actions[k].limit : false;
3940
var action = this.actions[k].do ? this.actions[k].do : 'click';
4041
var rmSelector = this.actions[k].rmSelector ? this.actions[k].rmSelector : null;
@@ -93,8 +94,15 @@ class UmbraBehavior {
9394
}
9495

9596
for ( var i = 0; i < repeats; i++) {
96-
if (!repeatSameElement && this.alreadyDone.indexOf(doTargets[i]) > -1) {
97-
continue;
97+
if (this.alreadyDone.indexOf(doTargets[i]) > -1) {
98+
if (!repeatSameElement) {
99+
continue;
100+
}
101+
102+
// We were looping the same element but we're done now
103+
if (repeatSameElement && repeatUntilSelector && document.querySelector(repeatUntilSelector).length > 0) {
104+
break;
105+
}
98106
}
99107
if (!this.isVisible(doTargets[i])) {
100108
continue;

0 commit comments

Comments
 (0)