Skip to content

Commit 0ecaa38

Browse files
authored
Fix custom behavior class example in docs (#940)
Updates custom behavior sample class and examples to be accurate: - Include missing required `init()` method - Fix arguments in examples uses of `Lib.getState()`
1 parent e320908 commit 0ecaa38

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

docs/docs/user-guide/behaviors.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ class MyBehavior
140140
return window.location.href === "https://my-site.example.com/";
141141
}
142142

143+
// required: typically should be left as-is.
144+
// must return an object, with optional fields:
145+
// state: passes fixed state to behavior context (ctx)
146+
// opts: passes options to behavior context (ctx). this may be
147+
// useful when injecting behaviors directly into the browser, and
148+
// provides a basis for passing options to custom behaviors from
149+
// browsertrix and archiveweb.page in the future.
150+
static init() {
151+
return {};
152+
}
153+
143154
// optional: if true, will also check isMatch() and possibly run
144155
// this behavior in each iframe.
145156
// if false, or not defined, this behavior will be skipped for iframes.
@@ -160,11 +171,11 @@ class MyBehavior
160171
// When the iterator finishes, the behavior is done.
161172
// (See below for more info)
162173
async* run(ctx) {
163-
//... yield ctx.getState("starting behavior");
174+
//... yield ctx.Lib.getState(ctx, "starting behavior");
164175

165176
// do something
166177

167-
//... yield ctx.getState("a step has been performed");
178+
//... yield ctx.Lib.getState(ctx, "a step has been performed");
168179
}
169180
}
170181
```
@@ -238,10 +249,10 @@ Using this standard function, the above code might be condensed as follows:
238249
if (elem.clickTwice) {
239250
elem.click();
240251
elem.click();
241-
yield Lib.getState("Double-Clicked on elem", "dblClick");
252+
yield Lib.getState(ctx, "Double-Clicked on elem", "dblClick");
242253
} else {
243254
elem.click();
244-
yield Lib.getState("Single-Clicked on elem", "click");
255+
yield Lib.getState(ctx, "Single-Clicked on elem", "click");
245256
}
246257
}
247258
}

0 commit comments

Comments
 (0)