From 38f60eb2d4198aeec9a0af46a1be8fedab2b4496 Mon Sep 17 00:00:00 2001 From: Enrico Bacis Date: Mon, 13 Nov 2023 14:27:09 +0100 Subject: [PATCH] Allow rule creation in sample.dnr-rule-manager when no rule exists (#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. --- functional-samples/sample.dnr-rule-manager/manager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functional-samples/sample.dnr-rule-manager/manager.js b/functional-samples/sample.dnr-rule-manager/manager.js index abc530b1..05d7b1ef 100644 --- a/functional-samples/sample.dnr-rule-manager/manager.js +++ b/functional-samples/sample.dnr-rule-manager/manager.js @@ -5,7 +5,7 @@ const viewRuleButton = document.getElementById('viewRuleButton'); async function getNextRuleId() { const rules = await chrome.declarativeNetRequest.getDynamicRules(); - return Math.max(...rules.map((rule) => rule.id)) + 1; + return Math.max(0, ...rules.map((rule) => rule.id)) + 1; } async function refresh() {