October 22, 2024
Chicago 12, Melborne City, USA
javascript

Chrome extension declarativeNetRequest, updateDynamicRules, id for new rule being added is never unique


I wanna make a chrome/edge extension that whenever a URL request is made (like googling, or entering something like a search or a site into the URL bar), checks whether it contains certain keywords, and blocks the request if it does.

So far all the syntax is correct, but I keep getting an error on the Rule.id property, and I cannot find out why.

(async () => {
  await chrome.declarativeNetRequest.updateDynamicRules(
    // options
    {
        // Rule[]
        addRules: [
            // Rule
            {
                action: {
                    type: "block"
                },
                // RuleCondition
                condition: {
                    // string
                    urlFilter: "*youtube|test1*"
                },
                id: 1
            }
        ]
    }
  );
})();

A "fix" is to remove the parameter parenthes at the end of the async function (because updateDynamicRules() is a promise), but then its not a function so nothing gets executed.
Another thing I’ve tried, which shows the problem isn’t the Rule.id itself, is setting the id as both 1, 2, and twice a random number – every time i get "Error: Rule with id x does not have a unique ID." and error pointer thing pointing at line 22, which btw is where the code ends.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video