如何使用chrome.identity.getProfileUserInfo在chrome.webRequest.onB



如何使用Chrome扩展API,可以使用回调函数,如chrome.identity.getProfileUserInfo,在chrome.webRequest.onBeforeRequest?

例如,我有一部分脚本类似于:

chrome.webRequest.onBeforeRequest.addListener(
function (request) {
return { redirectUrl: `https://example.com` };
},
// filters here. 
// permissions here. 
)

上面的工作,它重定向请求尝试到https://example.com

但是下面,将代码包装在回调函数中,return/redirectUrl部分被忽略,它不起作用;它不会重定向。

chrome.webRequest.onBeforeRequest.addListener(
function (request) {
chrome.identity.getProfileUserInfo(function (profileUserInfo) {
console.log(profileUserInfo.email);
return { redirectUrl: `https://example.com` };
});
},
// filters here. 
// permissions here. 
)

这是一个同步的方式,webRequest。onBeforeRequest只能够,对吗?我使用这些回调,同步的方式,因为我了解到chrome扩展API不支持异步的方式(例如async/await, Promise),因为我问在其他一天:javascript -异步在chrome. webrequest . onbeforerequest ?- Stack Overflow

所以,它是可能的,使用chrome.identity.getProfileUserInfo在chrome.webRequest.onBeforeRequest?谢谢。

不行。"chrome.identity.getProfileUserInfo"像大多数"铬"一样。方法需要时间去做,因此回调函数。"chrome.webRequest.onBeforeRequest"只支持"blocking"选项,而不是"asyncBlocking"但是在您的情况下,您可以轻松地将ProfileUserInfo预先分配给后台脚本中的某个变量,并像这样使用它。

最新更新