Skip to content

Commit 4ecb859

Browse files
authored
Issue 44584 Clarify filtering comments in background scripts page (#44592)
1 parent de5b264 commit 4ecb859

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

  • files/en-us/mozilla/add-ons/webextensions/background_scripts

files/en-us/mozilla/add-ons/webextensions/background_scripts/index.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ browser.runtime.onMessage.addListener(
164164

165165
### Filter events
166166

167-
Use APIs that support event filters to restrict listeners to the cases the extension cares about. If an extension is listening for {{WebExtAPIRef("tabs.onUpdated")}}, use the {{WebExtAPIRef("webNavigation.onCompleted")}} event with filters instead, as the tabs API does not support filters.
167+
When your extension only needs to act on a subset of events, such as when a web page is opened from a specific domain, use event filters where they are available.
168+
169+
For example, you can add a filter to the {{WebExtAPIRef("webNavigation.onCompleted")}} event to start your background script when certain URLs load, like this:
168170

169171
```js
170172
browser.webNavigation.onCompleted.addListener(
@@ -175,6 +177,8 @@ browser.webNavigation.onCompleted.addListener(
175177
);
176178
```
177179

180+
When targeting Firefox desktop only, you can also add filters to {{WebExtAPIRef("tabs.onUpdated")}} to achieve a similar outcome.
181+
178182
### React to listeners
179183

180184
Listeners exist to trigger functionality once an event has fired. To react to an event, structure the desired reaction inside the listener event.

0 commit comments

Comments
 (0)