declarativeNetRequest Redirect with parameters from original URL

325 views
Skip to first unread message

MV3 Noob

unread,
Sep 4, 2023, 1:55:39 AM9/4/23
to Chromium Extensions
Hello

i am trying to set a redirect rule using declarativeNetRequest with parameters from the original url :


I suspect its done using a regex filter but i fail at creating it, your help would me much appreciated.

Oliver Dunk

unread,
Sep 4, 2023, 5:18:11 AM9/4/23
to MV3 Noob, Chromium Extensions
Hi,

If the order of the query parameters is fixed, you could do something like this:

```
[
  {
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "regexSubstitution": "https://new.example.com/new.php?newparams=\\1&param2=\\2"
      }
    },
    "condition": {
      "regexFilter": "https:\/\/test.example.com\/page.php\\?param1=(.*)&param2=(.*)&param3=.*",
      "resourceTypes": ["main_frame"]
    }
  }
]
```

Here's a test extension: example.

I can't immediately think of a way to do this if the query parameters are in an arbitrary order. One thing you could try is redirecting to an extension page which then performs an additional redirect after looking at the query parameters.

Hope that helps!
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/285cc7a7-d342-44f3-9f85-d06db308a7d7n%40chromium.org.

Manuel Delgado

unread,
Sep 4, 2023, 8:55:52 AM9/4/23
to Chromium Extensions, Oliver Dunk, Chromium Extensions, MV3 Noob
Hi,

I have a problem when the request to the server of a tab is slower than in another tab.
Example:
Tab 1 -> Apply Rules Redirect -> send request but has not yet been sent to server
Open New Tab:
Tab 2 -> Apply Rules Redirect -> Send request and resolves. At that very moment, tab1 send request, apply rules the tab2 and not apply rules tab1.

How to solving the problem, any idea ?

Thanks.

Icono de Validado por la comunidad

Oliver Dunk

unread,
Sep 4, 2023, 9:06:45 AM9/4/23
to Manuel Delgado, Chromium Extensions, MV3 Noob
Hi,

I'm not certain that I fully understand the flow of everything.

Do you only add rules to certain requests based on activity in other tabs?

Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB
Message has been deleted
Message has been deleted

Manuel Delgado

unread,
Sep 5, 2023, 2:58:31 AM9/5/23
to Chromium Extensions, Oliver Dunk, Chromium Extensions, MV3 Noob, Manuel Delgado
No, I explain my case better. 

If I understand how the rules work correctly, the rules will apply in the future. So I have to anticipate the navigation for the rule to be applied, in this case the redirect. 

Well, I assign a numerical value to each created tab, which I store in the serviceworker, applying the redirection rule and injecting the numerical value that corresponds to the tab in the navigations. It does not apply to all URLs, the rule is conditioned by regular expressions. I give an example of task: 

Tab ID(33627819) -> (numerical value) 1 
Tab ID(33627820) -> (numerical value) 2 

Using the chrome.tabs(onCreated, onActivate) methods, I apply the rules using updateDynamicRules. There are times that one tab takes longer than the other to send request. If we are in the 33627819 tab, apply the redirection rule to inject the assigned numeric value, but it takes time to send the request, at that moment we change to the tab 3362720 assigning new redirection rule and we navigate. 

Just at that moment tab 33627819 sends request, therefore tab 33627820's rule is applied and not yours. 

Do you understand the use case better?

My doubt is, how to can i control this use case ? I do not understand because tab, 33627819, late too much in sends request. This case is random and I can't prove its behavior. 

Thank you

wOxxOm

unread,
Sep 5, 2023, 3:40:20 AM9/5/23
to Chromium Extensions, Manuel Delgado, Oliver Dunk, Chromium Extensions, MV3 Noob
Sounds like you want to imitate webRequest via declarativeNetRequest, which is generally impossible if you don't control the navigation, but if you'll describe the entire workflow of your extension in more detail maybe we'll find a better solution. Also show/explain how you send "requests" because it's unclear why different tabs take different time to send it e.g. if this is a content script then maybe all you need is to add "run_at": "document_start".

Manuel Delgado

unread,
Sep 5, 2023, 4:15:02 AM9/5/23
to Chromium Extensions, wOxxOm, Manuel Delgado, Oliver Dunk, Chromium Extensions, MV3 Noob

Add code:

Although save information in session storage, i work with variables local. I save information in session because i have a contentScript in other use case need the information. But now not affecting. I am going to try test to document_start to ContentScript.

let flagActivated = false;
let flagClearSession = true;
let removeTabs = {};
let tabUrls = {};

let tabsBlock = {};
let tabsRedirect = {};
let lastValue = 0;

chrome.tabs.onCreated.addListener((tab) => {
  console.log("tabsReditect onCreated", tabsRedirect);
  tabsRedirect[tab.id] = lastValue;
  applyRulesRedirect(lastValue);
  saveTabsInStorageRedirect(tabsRedirect, lastValue);
  flagActivated = true;
  lastValue++;
});

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  tabUrls[tabId] = tab.pendingUrl ? tab.pendingUrl : tab.url;
  console.log("onUpdated tabsUrls ", tabUrls);
  const url = tab.pendingUrl ? tab.pendingUrl : tab.url;
  if (
    Object.keys(tabsBlock).length + 1 <= MAX_TABS &&
    SITE_RULES.some((rule) => rule.test(url))
  ) {
    tabsBlock[tabId] = true;
    saveTabsInStorage(tabsBlock);
  }
});

chrome.tabs.onActivated.addListener((activeInfo) => {
  const tabId = activeInfo.tabId;

  if (!flagActivated) {
    console.log("He cambiado de pestaña");
    //REDIRECT
    applyRulesRedirect(tabsRedirect[tabId]);
  }

  flagActivated = false;

  // BLOCK
  console.log("onActivate ", tabsBlock[tabId]);
  if (Object.keys(tabsBlock).length >= MAX_TABS) {
    tabsBlock[tabId] === undefined
      ? applyRulesBlock(true)
      : applyRulesBlock(false);
  }
});

chrome.tabs.onRemoved.addListener(async (tabId, removeInfo) => {
  const actualTabs = await chrome.tabs.query({});
  const url = tabUrls[tabId];
  removeTabs[tabId] = url;
  delete tabUrls[tabId];
  console.log("onRemoved removeTabs ", removeTabs);
  if (!existTab(tabId, tabsBlock)) return;
  tabsBlock = deleteTab({
    myTabs: tabsBlock,
    isRedirect: false,
    actualTabs: actualTabs,
    lastValue: lastValue,
  });
  if (Object.keys(tabsBlock).length <= MAX_TABS) {
    applyRulesBlock(false);
  }
  tabsRedirect = deleteTab({
    myTabs: tabsRedirect,
    isRedirect: true,
    actualTabs: actualTabs,
    lastValue: lastValue,
  });
  if (flagClearSession) {
    flagClearSession = false;
    setTimeout(() => {
      console.log("clearSession ", removeTabs);
      clearSession(removeTabs);
      removeTabs = {};
      flagClearSession = true;
    }, 500);
  }
});

MV3 Noob

unread,
Sep 5, 2023, 6:45:31 AM9/5/23
to Chromium Extensions, Manuel Delgado, wOxxOm, Oliver Dunk, Chromium Extensions, MV3 Noob
just coming back to my original post, what if i only need to capture in the original url a single parameters or even only the value (foo) "param=foo" ? What would the regexSubstitution look like ? Unfortunately, this var could be placed in different places in the url.

MV3 Noob

unread,
Sep 5, 2023, 6:56:39 AM9/5/23
to Chromium Extensions, MV3 Noob, Manuel Delgado, wOxxOm, Oliver Dunk, Chromium Extensions
I found my answer on this post https://groups.google.com/a/chromium.org/g/chromium-extensions/c/qOc-NPzqu08 thanks again to W0xx0m as usual !
Reply all
Reply to author
Forward
0 new messages