0

I'm trying to append the matched domain from a declarativeNetRequest rule to the redirect extension page, but I can't seem to be able to get it to work. The redirect is working to my extension page but the matched URL isn't appended.

Here is my code snippet:

  const page = chrome.runtime.getURL('/MyPage.html');
  const RULES = [
  {
    'id': 1,
    'priority': 2,
    action: {type: 'redirect', redirect: {regexSubstitution: page + '#\\0', extensionPath: '/MyPage.html'}},
    'condition': {
      regexFilter: "\w*",
      requestDomains: ["amazon.com"]
    }
  }]

  chrome.declarativeNetRequest.updateDynamicRules({
    removeRuleIds: RULES.map(r => r.id),
    addRules: RULES,
  });

Updated code snippet:

    const page = chrome.runtime.getURL('/MyPage.html');
    const RULES = [
    {
      id: 1,
      priority: 2,
      action: {type: 'redirect', redirect: {regexSubstitution: page + '#\\1' }},
      condition: {
        regexFilter: "https://([^/]+)",
        requestDomains: ["amazon.com"]
      }
    },
    ];
    chrome.declarativeNetRequest.updateDynamicRules({
      removeRuleIds: RULES.map(r => r.id),
      addRules: RULES,
    });
Jason
  • 3
  • 2

1 Answers1

0
  1. Remove , extensionPath: '/MyPage.html' as you already have a substitution
  2. Replace \w* with ://([^/]+) to capture dots and dashes in the domain name, also note that inside regexp strings you need to use an escaped backslash \\ not \ while there's no need to escape the forward slash /.
  3. replace #\\0 with #\\1 to get the parenthesized group of the above regexp.
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • I've updated my code to use what you suggested and it's now showing "Open xdg-open?" dialog when a rule is matched (I'm on Ubuntu) not sure what is happening.. it's not opening the extension html page anymore either – Jason Feb 13 '23 at 00:16
  • Did you list MyPage in web_accessible_resources? Also see [this example](https://stackoverflow.com/a/73392754). – wOxxOm Feb 13 '23 at 00:56
  • I do: "web_accessible_resources": [{ "resources": ["MyPage.html"], "matches": [""] }] – Jason Feb 13 '23 at 01:09
  • If my linked example doesn't work for you either it may be a [bug in Chrome](https://crbug.com). – wOxxOm Feb 13 '23 at 11:51
  • Thanks for your help, I'll raise a bug in chrome for this. – Jason Feb 14 '23 at 22:36