首页 > 解决方案 > How do i get referer from chrome extension webRequest api?

问题描述

I'm trying to get complete referer from chrome extension's webRequest api. This is how my function looks like. I need to find alternative to details.initiator, as it's not enough precise.

//Listner to the onBeforeRequest event from webRequests api 
chrome.webRequest.onBeforeRequest.addListener(
    function(details) {

        //Checks if details url contains pixel.domain.com substring
        if (details.url.toLowerCase().indexOf('pixel.domain.com') > 0) {

            //Checks if map already contains array for this initiator
            if (pixels.get(details.initiator)) {
                var urls = pixels.get(details.initiator);
                if (!urls.includes(details.url)) {
                    pixels.set(details.initiator, [...urls, details.url]);
                }
            } else {
                //Else sets new (key, value)
                pixels.set(details.initiator, [details.url]);
            }
        }
    },
    { urls: ['<all_urls>'] }
);

Update: It can be also alternative to webRequest api, as i just need something, what tracks all requests made by the browser. Was considering also onRequest, but i'm not sure if it's not deprecated.

标签: javascriptgoogle-chromegoogle-chrome-extensionchrome-webrequest

解决方案


I found a solution. Instead onBeforeRequest, I use onBeforeSendHeaders, where I have access to the request headers


推荐阅读