使用Firefox本机消息进行异步webRequest.onBeforeRequest URL检查 [英] Asynchronous webRequest.onBeforeRequest URL inspection using Firefox native messages

查看:104
本文介绍了使用Firefox本机消息进行异步webRequest.onBeforeRequest URL检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Firefox本机消息创建URL检查器.问题是,当本机应用程序发送判决时,onBeforeRequest侦听器已经释放了请求,因此不会发生重定向. 如果答案为"0",您能否帮助我的分机等待最多2秒钟的回复并重定向请求?

I'm trying to create URL inspector using Firefox native messages. The problem is, when native application sends a verdict, onBeforeRequest listener already released request, thus redirection doesn't happen. Can you please help to make my extension wait for reply for up to 2 seconds and redirect the request if answer is "0"?

var port = browser.runtime.connectNative("ping_pong");

function inspectURL(requestDetails) {
    console.log("Loading: <" + requestDetails.url + ">");
    port.postMessage(requestDetails.url);
    console.log("Posting complete <" + requestDetails.url + ">");
}

port.onMessage.addListener((response) = > {
    console.log("Received: <" + response + ">");
    if (response == "1")
    {
        console.log("Good url!!!");
    }
    else
    {
        console.log("BAD url - redirecting!!!");
        return {
        redirectUrl: "https://38.media.tumblr.com/tumblr_ldbj01lZiP1qe0eclo1_500.gif"
        };
    }
});

browser.webRequest.onBeforeRequest.addListener(
    inspectURL,
    { urls: ["<all_urls>"] },
    ["blocking"]
);

推荐答案

根本没有办法.从Firefox 52开始,这是可能的,请参见

There's no way at all. It's possible since Firefox 52, see this answer. It's still not possible on Chrome.

本地消息传递是异步API.发布消息后,直到返回事件循环(例如,当前代码终止),您才会收到回复.

Native Messaging is an asynchronous API. After posting a message, you will not receive a reply until you go back to the event loop (e.g. current code terminates).

但是,阻止WebRequest API要求同步答复.这是一个核心限制,因为异步应答可能永远不会来临或在不确定的延迟之后出现,并且网络堆栈不会等待这种情况发生.我的意思是,可以,但是API的设计是故意禁止的.

However, blocking WebRequest API requires a synchronous reply. This is a core limitation, because asynchronous reply may never come or come after an uncertain delay, and the network stack won't wait for that to happen. I mean, it could, but the design of the API deliberately forbids it.

基本上:即使回复已准备好,您的代码也要等到inspectURL终止后才能收到,此时WebRequest已经继续执行请求. JavaScript中没有办法它是同步的.

Basically: even if the reply is ready, your code will not receive it until inspectURL terminates, at which point WebRequest already carries on with the request. There is no way in JavaScript to make it synchronous.

这篇关于使用Firefox本机消息进行异步webRequest.onBeforeRequest URL检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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