通过webextension的Firefox本地消息传递 [英] Firefox native messaging through webextension

查看:719
本文介绍了通过webextension的Firefox本地消息传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为firefox创建了一个webextension(目前使用Nightly 52),它使用本地消息在Linux上启动一个Java程序(Ubutu 14,32x)。加载webextension,读取.json文件并读取指向启动java程序的脚本的路径。 JSON和路径是正确的,因为当我使用

  var native = browser.runtime.connectNative(passwordmanager); 
console.log(native.name+ native.name); //输出passwordmanager。
native.onDisconnect.addListener(function(m){
console.log(Disconnected);});

上面的代码打印本地端口的名称,并打印Disconnected。所以我猜猜本地应用程序正在终止出于某种原因。应用程序现在只是骨架,只是执行sysout并读取sysin并正常工作,如果直接通过shell脚本启动它。
在调试webextension的时候,我不能进入到connectNative的调用中,因为它仅仅是调用这个调用,而不是介入。所以出于选择什么是错的。请让我知道,如果任何人能够创建一个本地消息应用程序基于FF webextension和任何指针我可能做错了。谢谢

解决方案

这里的解决方案展示了如何检测onConnect和onFail。它应该帮助你找出你真正的问题。所以我不认为你可以做适当的错误处理与 connectNative 。你可以做一些错误处理,如果你涉及的exe的一面,但你不能得到一个错误发生时的错误原因的字符串。错误只记录到控制台。



首先确保设置您的deeloper首选项,以便在浏览器控制台中显示消息。你可以使用这个插件 - https://addons.mozilla.org/en- US / firefox / addon / devprefs / - 或者阅读那个插件描述,它给你提供了设置首选项的MDN页面。

然后这就是你可以做一些错误处理(没有错误原因)(伪代码 - 我可能需要一个 .bind 在callbcks中):

 函数connectNative(aAppName,onConnect,onFail){
var listener =函数(有效载荷){$ b $ if(!connected){
connected = true;
port.onDisconnect.removeListener(failedConnect);
onConnect();
} else {
//处理消息
}
}
var failedConnect = function(){
onFail('失败,原因不可达 - 浏览器控制台,因为它记录在那里');
}
var connected = false;
var port = chrome.runtime.connectNative(aAppName);
port.onMessage.addListener(listener);
port.onDisconnect.addListener(failedConnect);
返回端口;





$ b现在在你的exe文件中,只要启动,就把它写入stdout的东西。这将触发 onConnect


Created a webextension for firefox (currently using Nightly 52), that uses native messaging to launch a java program on linux (Ubutu 14, 32x). The webextension loads , reads the .json file and reads the path which points to a script that starts the java program . The JSON and the path are correct as when I use

var native = browser.runtime.connectNative("passwordmanager");
console.log("native.name" + native.name); //outputs passwordmanager.
native.onDisconnect.addListener(function(m){
                                           console.log("Disconnected"); });

the above code prints the name of the native port and also prints "Disconnected". So I m guessing the native app is terminating for some reason . The application is only skeleton right now that just does sysout and reads sysin and works correctly if Launch it directly through the shell script. While debugging the webextension, I am not able to step into the call to connectNative, as it just steps-over that call instead of doing step-in . So kind of out of options whats' going wrong. Please let me know if any one is able to create a native messaging app based on FF webextension and any pointers on what I might be doing wrong. Thanks

解决方案

This solution here shows you how to detect onConnect and onFail. It should help you out to figure out your real problem.

So I don't think you can do proper error handling with connectNative from the JS side alone. You can do somewhat error handling if you get the exe side involved, but you can't get a string for "error reason" when an error occurs. The error is only logged to console.

First make sure to set your deeloper prefs, so messages show in your browser console. You can use this addon - https://addons.mozilla.org/en-US/firefox/addon/devprefs/ - or read that addon description it gives you the MDN page with the prefs to set.

Then this is how you can do some sort of error handling (without error reason) (pseudo-code - i might need a .bind in the callbcks):

function connectNative(aAppName, onConnect, onFail) {
    var listener = function(payload) {
        if (!connected) {
            connected = true;
            port.onDisconnect.removeListener(failedConnect);
            onConnect();
        } else {
            // process messages
        }
    }
    var failedConnect = function() {
        onFail('failed for unattainable reason - however see browser console as it got logged there');
    }
    var connected = false;
    var port = chrome.runtime.connectNative(aAppName);
    port.onMessage.addListener(listener);
    port.onDisconnect.addListener(failedConnect);
    return port;
}

Now in your exe, as soon as it starts up, make it write to stdout something. That will trigger the onConnect.

这篇关于通过webextension的Firefox本地消息传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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