Skip to content

Commit 38f60eb

Browse files
authored
Allow rule creation in sample.dnr-rule-manager when no rule exists (GoogleChrome#1029)
The `functional-samples/sample.dnr-rule-manager` example allows the user to add and remove `chrome.declarativeNetRequest` rules on demand from the extension's options page. Rules require to have a unique `id`, so the extension fetches the list of all the currently defined rules, takes the max and adds 1. When no rule is defined, this results in `Math.max(...[]) + 1` => `Math.max() + 1` => `-Infinity + 1` => `-Infinity` and since the `id` should be an integer and not an number, the extension throws an error. Ensuring that `0` is always a parameter of `Math.max` should solve the problem.
1 parent 5be3966 commit 38f60eb

File tree

1 file changed

+1
-1
lines changed
  • functional-samples/sample.dnr-rule-manager

1 file changed

+1
-1
lines changed

functional-samples/sample.dnr-rule-manager/manager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const viewRuleButton = document.getElementById('viewRuleButton');
55

66
async function getNextRuleId() {
77
const rules = await chrome.declarativeNetRequest.getDynamicRules();
8-
return Math.max(...rules.map((rule) => rule.id)) + 1;
8+
return Math.max(0, ...rules.map((rule) => rule.id)) + 1;
99
}
1010

1111
async function refresh() {

0 commit comments

Comments
 (0)