DNR dynamic redirect cache problem

134 views
Skip to first unread message

Manuel Delgado

unread,
Jun 7, 2023, 12:43:43 PM6/7/23
to Chromium Extensions

Hi, I'm trying to develop an extension that does redirections to add queryParams to specific URLs. I'm using two APIs:

  • chrome.webRequest for request interception. 
  • DNR (Declarative Net Request) for redirection. I am using the method updateDynamicRules for adding rules and removig rules, thus updating the same rule always.

The problem is that once I do a redirection for a host, the next redirection for the same host does not apply the new query params defined in the updated rule, but the defined in the older rule before removing and adding it dynamically.

I had tried thigs such as generating a timestamp for having always different values for the query params but it still caches the rule values somehow.

Also, I've tried clearing cache with different options as indicated on the API DNR documentation https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#implementation-cached-pages but it's not working.

Any idea why rules are not always upgraded properly?

Manuel Delgado

unread,
Jun 7, 2023, 12:45:33 PM6/7/23
to Chromium Extensions, Manuel Delgado

I am using MV3.
Message has been deleted

Manuel Delgado

unread,
Jun 9, 2023, 3:36:13 AM6/9/23
to Chromium Extensions, Manuel Delgado
I do add code example. Is posible that implementation does not corret, any suggestion will well received.

import { getTabs, saveTab } from "../storage/storage.js";

chrome.webRequest.onBeforeRequest.addListener(async (details) => {    
    const tabsOpen = await getTabsOpen();//indicate if you have tabs number open with specify pathUrl in pedingUrl or Url attribute of tab.
    if (tabsOpen) {        
        //Here there are a problem, update tab does two forward, first the url original after the url information in method "tabs.update".
        //Is posible that "await" here not applyed, is a test. But is not causing doing the two forward, without "await" still does.
        await chrome.tabs.update(details.tabId, {url: "chrome://newtab"});                          
    } else if (details.url.includes("pathUrl") && !details.url.includes("pathUrl") && !details.url.includes("pathUrl")) {
        await saveTab(details.tabId);//save (session storage) a relationship between new tab open and value (queryParam) that will be added in url . Value is a autoincremental number.
        const actualTabs = await getTabs();//obtain all tabs saved in session storage
        const value = await getvalueByTabId(actualTabs, details.tabId);//obtain value queryParam to inject, calculated in saveTab.
        console.log(`Url inicial ${details.url}`);
        console.log(details);
        if (!details.url.includes("param") && !tabsOpen) {
            console.log("Url resultante: ", details.url + (details.url.indexOf("?") == -1 ? "?" : "&" ) + `param=${value}`);
            await injectParams(value);//inject queryParam through DNR rules    
        } else if (!tabsOpen) {
            /**
             * This "if" exitst because there are cases in that the new tab open inherits param=value from tab previous. I am trying force new value in queryParam, but not working.
             */
            const actualvalue = getActualvalue(details.url);
            if (actualvalue != value) {
                console.log(`Param de la url actual ${actualvalue} actualizamos a ${value}`);
                await injectParams(value);
            }
        }
    }

}, {urls: ["filterUrl"], types: ["main_frame"]}, undefined);

async function injectParams(value){  
    //Here i am trying apply rule to redirect. Also, i had tried change order, first clear cache, after, add and remove rule, but not working.
    chrome.declarativeNetRequest.updateDynamicRules({
        addRules: [
            {
                "id": 1,
                "priority": 1,
                "action": {
                    "type": "redirect",                        
                    "redirect": {
                        "transform": {
                            "queryTransform": {
                                "addOrReplaceParams": [{
                                    "key": "param",
                                    "value": value
                                }]
                            }
                        }
                    }
                },
                "condition": {
                    "urlFilter": "urlPath",
                    "resourceTypes": ["main_frame"]
                }
            }
        ],
        removeRuleIds:[1]
    });          
    await chrome.browsingData.remove(
    {
        "origins": ["filterUrl"]
        }, {
        "cacheStorage": true
    });
}

function getActualvalue (url) {
    const queryParams = url.split('?');
    const searchParams = new URLSearchParams(queryParams[1]);
    return searchParams.get("param");
}

async function getvalueByTabId (actualTabs, tabId) {
    return Object.keys(actualTabs).find(key => actualTabs[key] === tabId);
}

async function getTabsOpen() {
    console.log("all tabs", (await chrome.tabs.query({})).length);
    const all_tabs = await chrome.tabs.query({});
    const num_tabs_open = all_tabs.filter((value) => {
        return (value.pendingUrl && value.pendingUrl.includes("pathUrl")) || value.url.includes("pathUrl");
    });
    console.log("tabs open ",num_tabs_open.length);
    return num_tabs_open.length > 7;
}

Thanks

wOxxOm

unread,
Jun 9, 2023, 11:46:07 AM6/9/23
to Chromium Extensions, Manuel Delgado
Sounds like you expect declarativeNetRequest to apply to the current request, but it doesn't work like this - it applies the rules to new requests in the future.

The solution is to create the rule when the conditions change, which is the number of tabs in your case. You can observe chrome.tabs.onUpdated, calculate the number of tabs inside, then update declarativeNetRequest accordingly, see this example.

Manuel Delgado

unread,
Jun 12, 2023, 6:03:33 AM6/12/23
to Chromium Extensions, wOxxOm, Manuel Delgado
Hi,

I will review and i will try apply the solution in my code. I will report the result.

Thanks.

Manuel Delgado

unread,
Jun 13, 2023, 10:18:32 AM6/13/23
to Chromium Extensions, Manuel Delgado, wOxxOm

Hi,

I have reviewed the example. But now it is not working in a specific case. 

ShowCase:
1 - Open Google Chrome
2 - Access the url (configured in rules)
3 - From this page, right click on any link. Open the link in a new tab
4 - The rule is not applying because request is faster than callback of event "onCreated". 

The url of the new tab is keeping the queryParam value of previous tab.

Thanks.
import { getTabs, saveTab, removeTabs, getvalueByTabId } from "../storage/storage.js";

chrome.runtime.onInstalled.addListener( (details) => {
    if (details.reason.match("install") || details.reason.match("update")) {
        chrome.browsingData.removeCacheStorage({}, () => {
            console.log("Caché borrada");
        });        
    }
});
chrome.tabs.onCreated.addListener(async (tabId) => {
    console.log("onCreated ", tabId);
    applyRules(tabId);    
});

chrome.tabs.onUpdated.addListener(async (tabId) => {
    console.log("onUpdated ", tabId);
    applyRules(tabId);    
});

chrome.tabs.onRemoved.addListener( async (tabId) => {
    console.log("onRemoved ", tabId);
    await removeTabs(tabId);
    const tabActive = await chrome.tabs.query({active: true});
    applyRules(tabActive[0].id);

});

chrome.tabs.onActivated.addListener(async (activeInfo) => {
    console.log("onActivated ", activeInfo.tabId);
    applyRules(activeInfo.tabId);
});

const applyRules = async (tabId) => {
    await saveTab(tabId);
    const actualTabs = await getTabs();
    const value = await getvalueByTabId(actualTabs, tabId);  
    chrome.declarativeNetRequest.updateDynamicRules({
        addRules: [
            {
                "id": 1,
                "priority": 1,
                "action": {
                    "type": "redirect",                        
                    "redirect": {
                        "transform": {
                            "queryTransform": {
                                "addOrReplaceParams": [{
                                    "key": "param1",
                                    "value": value
                                }]
                            }
                        }
                    }
                },
                "condition": {
                    "urlFilter": "url",
                    "resourceTypes": ["main_frame"]
                }
            }
        ],
        removeRuleIds:[1]
    });
}

wOxxOm

unread,
Jun 13, 2023, 10:22:01 AM6/13/23
to Chromium Extensions, Manuel Delgado, wOxxOm
  Sounds like you expect declarativeNetRequest to apply to the current request, but it doesn't work like this - it applies the rules to new requests in the future. Your condition should be based on the current number of tabs i.e. when the maximum is reach you add the rule, this is what my example does, see the link in my previous message.
Message has been deleted

Omar Ali

unread,
Jun 13, 2023, 10:40:18 AM6/13/23
to Manuel Delgado, Chromium Extensions, wOxxOm
I can't understand you well. Can I communicate with you on WhatsApp?

في الثلاثاء، ١٣ يونيو ٢٠٢٣, ٤:٣٣ م Manuel Delgado <myk...@gmail.com> كتب:
I don't understand. If I open a new tab and navigate to url. This action, open new tab, already is apply rule and when i do press "enter" apply the rule. Why if the same proccess but launch from link not working?
--
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/a8973f65-36ad-4be2-b543-3813257072can%40chromium.org.

Manuel Delgado

unread,
Jun 13, 2023, 10:43:11 AM6/13/23
to Chromium Extensions, Omar Ali, Chromium Extensions, wOxxOm, Manuel Delgado
I don't understand. If I open a new tab from the upper bar, and navigate to a URL, is activating the rule and working fine. But if I open a tab by right-clickng on a link, the rule is not working. Shouldn't it work the same way?

Manuel Delgado

unread,
Jun 14, 2023, 4:08:05 AM6/14/23
to Chromium Extensions, Manuel Delgado, Chromium Extensions, wOxxOm
Any ideas why it doesn't work the same way?
Reply all
Reply to author
Forward
0 new messages