Declarative Net Request Issues: ResourceType Error and Browser Compatibility

380 views
Skip to first unread message

Tamar Y

unread,
Nov 1, 2023, 5:53:38 AM11/1/23
to Chromium Extensions
Hello Chrome Developers,

I am attempting to set the Referer header to a specific value using the chrome.declarativeNetRequest API. Here is the code snippet I've used:

chrome.runtime.onInstalled.addListener(async () => {
    await chrome.declarativeNetRequest.updateDynamicRules({
        removeRuleIds: [1],
        addRules: [{
            id: 1,
            priority: 1,
            action: {
                type: chrome.declarativeNetRequest.RuleActionType.MODIFY_HEADERS,
                requestHeaders: [
                    {
                        header: 'Referer',
                        operation: chrome.declarativeNetRequest.HeaderOperation.SET,
                        value: 'test'
                    },
                    {
                        header: 'Sec-Fetch-Site',
                        operation: chrome.declarativeNetRequest.HeaderOperation.SET,
                        value: 'same-origin'
                    }
                ]
            },
            condition: {
                urlFilter: '"|https*"',
                resourceTypes: [
                    chrome.declarativeNetRequest.ResourceType.MAIN_FRAME,
                    chrome.declarativeNetRequest.ResourceType.SUB_FRAME,
                    chrome.declarativeNetRequest.ResourceType.STYLESHEET,
                    chrome.declarativeNetRequest.ResourceType.SCRIPT,
                    chrome.declarativeNetRequest.ResourceType.IMAGE,
                    chrome.declarativeNetRequest.ResourceType.OBJECT,
                    chrome.declarativeNetRequest.ResourceType.XML_HTTP_REQUEST,
                    chrome.declarativeNetRequest.ResourceType.OTHER
                ]
            }
        }]
    });
});


However, I'm encountering the following error:

TypeError: Error in invocation of declarativeNetRequest.updateDynamicRules(declarativeNetRequest.UpdateRuleOptions options, optional function callback): Error at parameter 'options': Error at property 'addRules': Error at index 6: Error at property 'condition': Error at property 'resourceTypes': Error at index 6: Invalid type: expected declarativeNetRequest.ResourceType, found undefined.

It seems like there's an issue with the chrome.declarativeNetRequest.ResourceType.XML_HTTP_REQUEST, even though I've taken its value from the enum list.

But the more problematic issue is the point that I've noticed that this code is working as expected when running the extension in a browser with web security disabled using the flags --disable-web-security --disable-gpu. However, it does not work in regular browsers. Here is a snippet of my permissions from the manifest:


"permissions": [
    "activeTab",
    "storage",
    "tabs",
    "webNavigation",
    "alarms",
    "notifications",
    "nativeMessaging",
    "downloads",
    "unlimitedStorage",
    "cookies",
    "gcm",
    "webRequest",
    "sidePanel",
    "declarativeNetRequest",
    "declarativeNetRequestWithHostAccess",
    "declarativeNetRequestFeedback"
  ],
  "host_permissions": [
    "<all_urls>",
    "http://*/",
    "https://*/"
  ]

Did I miss something? 

Thank you for your assistance!

Oliver Dunk

unread,
Nov 1, 2023, 6:07:45 AM11/1/23
to Tamar Y, Chromium Extensions
Hi Tamar,

Is there a specific place where you found the enum list?

The correct value is chrome.declarativeNetRequest.ResourceType.XMLHTTPREQUEST.

I'm not sure why you saw those flags making a difference - I wouldn't expect those to be related.

Thanks,
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/55a50347-15f2-4815-acec-cbac506da7f4n%40chromium.org.
Reply all
Reply to author
Forward
0 new messages