原生邮件应用程序无法正常运行的firefox扩展

时间:2018-03-26 10:49:31

标签: firefox-addon chrome-native-messaging

我有一个用Java开发的本机应用程序(编译的jar),该扩展基本上使用用户输入将消息发送到本机应用程序,并将响应返回到网页上的标签中。

扩展程序在Chrome上工作正常,但我无法在Firefox 58.0.1(量子64位)中执行相同的操作。

浏览器控制台出错。

无法将null转换为对象

下面的

是代码段和错误屏幕截图。

   {
    "manifest_version":2,

    "name":"Firefox Automation Extension",
    "version":"1.0",
    "description":"Automation Extensions",

    "icons": {
      "16": "icons/synergy.png"
    },

    "browser_action":{

      "default_icon": {
       "32" : "icons/synergy.png"
      },
      "default_title": "Native Messaging Application Testing",
      "default_popup": "index.html"      
},
    "background":{
    "scripts": ["background.js"]
    },

    "applications": {
        "gecko": {
          "id": "webdom@oracle.com",
          "strict_min_version": "58.0"
        }
      },
        "content_security_policy":"script-src 'self' 'unsafe-eval'; object-src 'self'",

    "permissions": [
      "nativeMessaging", "<all_urls>"
    ],

    "content_scripts": [
        {
          "matches": [
            "<all_urls>"
          ],
          "js": [
            "jquery-1.12.2.min.js", 
            "contentScript.js"
          ]
        }
      ]

}

/ * background.js * /

browser.runtime.onMessage.addListener(function(msg,sender,sendResponse){     console.info(“从%o收到%o,frame”,msg,sender.tab,sender.frameId);

// As it is registered in registy
var host_name="xxxxxxxxxxxxxxxxxxx";

// Open port (for communication).
var port = browser.runtime.connectNative(host_name);

// Send message to native application.
port.postMessage(msg);

// Listen for response...
port.onMessage.addListener(function (msg) {
    // Send data to the content.
    browser.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        browser.tabs.sendMessage(tabs[0].id, msg, function (response) { });
    });
});

port.onDisconnect.addListener(function () {
    console.info("Disconnected.");
});

});

/ * contentScript.js * /

document.addEventListener("send-message-event", function (data) {

    var request = data.detail.data;
    console.log("content script : ", request);
    // Send message to the background script
    browser.runtime.sendMessage(request, null);
});

/**
 * Listens to the background script and dispatches 'get-message-event' 
 * to the client when the data is received.
 */
browser.runtime.onMessage.addListener(function (response, sender, sendResponse) {
    console.log(response);
    // Send response to the front page
    var event = new CustomEvent("get-message-event", {
        detail: {
            data: response
        },
        bubbles: true,
        cancelable: true
    });
    document.dispatchEvent(event);
});

/ * Main.js(从html页面调用的脚本)* /

$(document).ready(function () {

    var sendMessageBtn = $('#send-message-button');
    var inputElem = $('#input-text');
    var responseElem = $('#response');

    /**
     * Send message operation
     */
    sendMessageBtn.click(function () {
        var request = {};
        request.message = inputElem.val();

        var event = new CustomEvent("send-message-event", {
            detail: {
                data: request
            },
            bubbles: true,
            cancelable: true
        });

        console.log("From Main : ",event.detail.data);
        document.dispatchEvent(event);
    });

    /**
     * Get message event listener
     */
    document.addEventListener("get-message-event", function (data) {
        var responseObject = data.detail.data;
        responseElem.text(responseObject.message);
    });
});

index.html页面

<!DOCTYPE html>
<html>

<head>
    <title>Native Messaging</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="bootstrap.min.css">
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-md-4"></div>
            <div class="col-md-4">
                <div class="page-header">
                    <h2>Native Messaging</h2>
                </div>
                <div class="form-group">
                    <label for="input-text">Input message:</label>
                    <input class="form-control" id='input-text' type='text' value="Test" />
                </div>
                <button type="button" class="btn btn-block btn-default" id='send-message-button'>Send Message</button>
                <hr>
                <div class="well well-lg" id='response'>Response from Native app...</div>
            </div>
            <div class="col-md-4"></div>
        </div>

    </div>
    <!-- jQuery and JS files -->
    <script src="jquery-1.12.2.min.js"></script>
    <script src="main.js"></script>
</body>

</html>

点击发送消息按钮时,我无法将null转换为对象错误。

代码在Chrome中正常运行,我可以从Chrome扩展程序中的Native应用程序获得响应

0 个答案:

没有答案