Google Chrome扩展程序中的同步呼叫 [英] Synchronous call in Google Chrome extension

查看:109
本文介绍了Google Chrome扩展程序中的同步呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Google Chrome扩展程序,该扩展程序必须阻止/重定向某些传出的请求。为此,我使用 chrome.webRequest.onBeforeRequest 侦听器。
要决定是否阻止请求,我需要一些关于从中发送的选项卡请求的信息。我可以使用 chrome.tabs.get(整型tabId,函数回调)来获取它,但回调是异步的,这意味着它可能会在 onBeforeRequest 侦听器。

  chrome.webRequest.onBeforeRequest.addListener(function(details) {
chrome.tabs.get(details.tabId,函数(标签){
//从标签
获取信息));
//根据标签中的信息返回重定向或者不是
}),{
url:[< all_urls>],
types:[main_frame]
},[blocking]);

有没有办法同步通话?或者可能还有其他选项。

解决方案

堆栈溢出的另一个答案建议跟踪监听器函数的外部,这完全避免了这个问题。



示例代码:

  / * 
* --------- -----------------------------------------
*保留标签列表请求回调之外
* ----------------------------------------- ---------
* /
var tabs = {};

//获取所有现有标签
chrome.tabs.query({,function(results){
results.forEach(function(tab){
tabs) [tab.id] = tab;
});
});

//创建标签事件监听器
函数onUpdatedListener(tabId,changeInfo,tab){
tabs [tab.id] = tab;
}
函数onRemovedListener(tabId){
删除标签[tabId];
}

//订阅标签事件
chrome.tabs.onUpdated.addListener(onUpdatedListener);
chrome.tabs.onRemoved.addListener(onRemovedListener);

/ *
* ----------------------------------- ---------------
*请求回调
* ----------------------- ---------------------------
* /
//创建请求事件侦听器
function onBeforeRequestListener(详细信息){
// ***请记住,tabId可以设置为-1 ***
var tab = tabs [details.tabId];

//响应标签信息
}

//订阅请求事件
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequestListener,{
urls:[< all_urls>],
types:[main_frame]
},[blocking]);


I'm working on Google Chrome extension, which has to block/redirect some outgoing requests. For this purpose, I use chrome.webRequest.onBeforeRequest listener. To decide, whether to block request or not, I need some information about the tab request is sent from. I can get it using chrome.tabs.get(integer tabId, function callback), but callback is asynchronous, which means it may be called after the value is returned from onBeforeRequest listener.

chrome.webRequest.onBeforeRequest.addListener(function(details){
 chrome.tabs.get(details.tabId, function(tab){
  // get info from tab
 }); 
 // based on info from tab return redirect or not
}), {
 urls: ["<all_urls>"],
 types: ["main_frame"]
}, ["blocking"]);

Is there a way to synchronize the call? Or maybe some other option.

解决方案

Another answer on Stack Overflow recommends keeping track of the tabs outside of your listener function, which avoids this problem entirely.

Example code:

/* 
 * --------------------------------------------------
 * Keep list of tabs outside of request callback
 * --------------------------------------------------
 */
var tabs = {};

// Get all existing tabs
chrome.tabs.query({}, function(results) {
    results.forEach(function(tab) {
        tabs[tab.id] = tab;
    });
});

// Create tab event listeners
function onUpdatedListener(tabId, changeInfo, tab) {
    tabs[tab.id] = tab;
}
function onRemovedListener(tabId) {
    delete tabs[tabId];
}

// Subscribe to tab events
chrome.tabs.onUpdated.addListener(onUpdatedListener);
chrome.tabs.onRemoved.addListener(onRemovedListener);

/* 
 * --------------------------------------------------
 * Request callback
 * --------------------------------------------------
 */
// Create request event listener
function onBeforeRequestListener(details) {
    // *** Remember that tabId can be set to -1 ***
    var tab = tabs[details.tabId];

    // Respond to tab information
}

// Subscribe to request event
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequestListener, {
    urls: ["<all_urls>"],
    types: ["main_frame"]
}, ["blocking"]);

这篇关于Google Chrome扩展程序中的同步呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆