原生应用不适用于Chrome扩展程序

时间:2014-01-24 07:14:29

标签: google-chrome-extension chrome-native-messaging

我正在尝试使用适用于Chrome扩展程序的Chrome Native Messaging API。

原生app的Manifest.json:

{
  "name": "app.native",
  "description": "Native Message API Test.",
  "path": "native.exe",
  "type": "stdio",
  "allowed_origins": ["chrome-extension://kembignchdjhopkkcolnamikcenaocdm/"]
}

Windows注册表值:

HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\app.native=D:\connectNative\manifest.json

我也试过D:\\\\connectNative\\\\manifest.json

我在Chrome扩展程序manifest.json中将“nativeMessaging”添加到“权限”。

原生app cpp:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(int argc, char* argv[]) {
    string input = "";
    string message="{\"text\": \"This is a response message\",\"num\": \"three\"}";
    unsigned int len = message.length();
    cout << char(((len>>0) & 0xFF))
         << char(((len>>8) & 0xFF))
         << char(((len>>16) & 0xFF))
         << char(((len>>24) & 0xFF));
    cout << message <<endl;
    getline(cin, input);
    cout << "You entered: " << input << endl;
    ofstream myfile;
    myfile.open ("example.txt");
    myfile << "Writing this to a file.\n";
    myfile << input;
    myfile.close();

    return 0;
}

完成所有操作后,我尝试使用Chrome扩展程序:

var testport = chrome.runtime.connectNative('app.native');
testport.onMessage.addListener(function(msg) {
    console.log("Received" + msg);
});
testport.onDisconnect.addListener(function() {
  console.log("Disconnected");
});

它无法接收任何信息并始终打印“已断开连接”。

我尝试连接到一个不存在的应用程序,它仍然打印“Disconnected”,所以我知道这个原生应用程序没有正确配置。

任何人都可以指出错误或错过了什么吗?

2 个答案:

答案 0 :(得分:1)

默认情况下,cout是一个文本流,发送null(在前4个字节的大小中发生)会提前结束文本流。

在Windows上,您可以通过更改底层标准输出更新cout为二进制文件,并且不要忘记刷新...

 public uint pcieDeviceControlRegister
    {
      get
      {
        if (pcieCapabilityOffset != 0)
          return (ReadDW((int)(pcieCapabilityOffset + 8) / 4, 0xF)) & 0xFFFF;
        else
          return 0;
      }
      set
      {
        if (pcieCapabilityOffset != 0) 
        {
          uint val = ReadDW((int)(pcieCapabilityOffset + 8) / 4, 0xF)& 0xFFFF0000;
          val |= value;
          // write should be done with byte enables !!!
          WriteDW((int)(pcieCapabilityOffset + 8) / 4, val, 0xF);
        }
      }
    }

答案 1 :(得分:-1)

使用Native引用的工作脚本示例。请注意nativeMessaging的权限,Manifest.json中的.js引用中的外部资源不会直接引用{ "background": { "scripts": [ "common.js", "filler.js", "background.js" ] }, "browser_action": { "default_icon": "r.png", "default_title": "Click this button to show commands" }, "content_scripts": [ { "all_frames": true, "js": [ "common.js", "content.js", "filler.js" ], "matches": [ "http://*/*", "https://*/*", "file:///*" ], "run_at": "document_start" } ], "description": "For Google Chrome", "homepage_url": "http://www.app.com", "icons": { "128": "r.png", "16": "r.png", "32": "r.png", "48": "r.png" }, "key": "???", "manifest_version": 2, "name": "???", "options_page": "options.html", "permissions": [ "tabs", "bookmarks", "webRequest", "webRequestBlocking", "nativeMessaging", "downloads", "http://*/*", "https://*/*" ], "update_url": "https://clients2.google.com/???/", "version": "???" } 脚本。

{{1}}