Search code examples
google-chrome-extension

How to append to requestHeaders using declarativeNetRequest dynamic rules


From within a Chrome extension, I'm trying to create a Dynamic Rule that appends a string to the user agent via declarativeNetRequest as follow:

    chrome.declarativeNetRequest.updateDynamicRules({
      addRules: [
        {
          id: 1,
          priority: 1,
          action: {
            type: 'modifyHeaders' as RuleActionType,
            requestHeaders: [
              { 
                header: 'user-agent', 
                operation: 'append' as HeaderOperation, 
                value: '-test'
              },
            ],
          },
          condition: {
            regexFilter: 'https://www.yahoo.com\?.*',
            resourceTypes: [
              'main_frame' as ResourceType,
              'sub_frame' as ResourceType,
            ],
          },
        },
      ],
    }, async (result: any) => {
      console.log('created', result);
    });

And I'm getting the following error in the console:

Unchecked runtime.lastError: Rule with id 1 must not specify a request header to be appended.

Is there a restriction that I'm missing from the documentation where we can't apply the append operation on requestHeaders? I tested with responseHeaders and it worked ok (it's just not what I want to achieve).


Solution

  • It's a known bug, https://crbug.com/1117475.

    Meanwhile use set instead of append in the value of operation.