Google chrome extension 在chrome.webRequest.onBeforeRequest中获取文档url

Google chrome extension 在chrome.webRequest.onBeforeRequest中获取文档url,google-chrome-extension,Google Chrome Extension,在chrome.webRequest.onBeforeRequest中,我们得到各种url——javascript、css等 对于每个url,我想知道主选项卡url 同步获取数据的最简单方法是什么 现在我用这种方式: 如果details.frameId==0,则details.url包含此选项卡id的主选项卡url chrome.webRequest.onBeforeRequest.addListener( function (details) { i

chrome.webRequest.onBeforeRequest
中,我们得到各种url——javascript、css等

对于每个url,我想知道主选项卡url

同步获取数据的最简单方法是什么

现在我用这种方式:


如果
details.frameId==0
,则
details.url
包含此选项卡id的主选项卡url

chrome.webRequest.onBeforeRequest.addListener(
        function (details) {

            if (details.tabId == -1)
            {
                return;
            }


            if ("type" in details && ['main_frame', 'sub_frame'].indexOf(details.type) !== -1)
            {
                if (details.frameId == 0) {
                    all_tabs_info.add_tab_info(details.tabId, details.url);
                }
            }


        },
        {
            urls: ['<all_urls>']
        },
["blocking"]);
chrome.webRequest.onBeforeRequest.addListener(
功能(详情){
如果(details.tabId==-1)
{
返回;
}
if(“type”在details&[“main_-frame”,“sub_-frame”].indexOf(details.type)!=-1)
{
如果(details.frameId==0){
所有选项卡信息。添加选项卡信息(details.tabId,details.url);
}
}
},
{
网址:['']
},
[“封锁]);


因此,从现在开始,如果有任何关于这个标签id的请求,我们已经有了标签url。这一粗略的逻辑似乎正在发挥作用。

一些增强功能:

  • 在本例中,用于
    类型
  • main\u frame
    类型对应于顶级帧,这意味着它的
    frameId
    为0,因此通过不侦听
    sub\u frame
    可以完全忽略检查

    值0表示请求发生在主帧中;正值表示发生请求的子帧的ID。如果加载了(子)帧的文档(类型为main_-frame或sub_-frame),frameId表示该帧的ID,而不是外部帧的ID。框架ID在选项卡中是唯一的

  • 类型
    不是可选的,正如您在文档中看到的,无需怀疑它的存在

因此,简化代码如下:

chrome.webRequest.onBeforeRequest.addListener(
功能(详情){
如果(details.tabId>=0){
所有选项卡信息。添加选项卡信息(details.tabId,details.url);
}
},
{
网址:[''],
类型:[“主框架”],
},
[“封锁”]
);