2014-05-08 99 views
1

我正在開發一個google-chrome應用程序,我需要啓動一個Java應用程序。 (真的,我需要閱讀並保存沒有選擇文件彈出的文件)。Chrome應用程序:使用本地消息傳遞主機啓動外部應用程序(shell腳本或jar)

由於NPAPI庫已被廢棄,我尋找替代品,並決定使用「Native messaging hosts」啓動外部應用程序。

爲了創建我的第一個示例,我試圖啓動一個shell腳本,因爲我認爲這更容易。但是我還沒有設法啓動腳本。我開發在Linux

清單文件是在這條道路

「/etc/opt/chrome/native-messaing-hosts/com.centeropenmiddleware.l3p1.xmleditor.json」:

和包含的是:

{ 
    "name": "com.centeropenmiddleware.l3p1.xmleditor", 
    "description": "Saving a file", 
    "path": "/home/paco2/pp.sh", 
    "type": "stdio", 
    "allowed_origins": [ 
    "chrome-extension://plfnjepfbddljeogeacemcpceiofapnm/" 
    ] 
} 

應用程序ID是plfnjepfbddljeogeacemcpceiofapnm

腳本創建一個文件:

#!/bin/bash 
echo hola mundo >> aaa 

應用程序代碼在此行失敗:

try { 
    var port = chrome.runtime.connectNative ('com.centeropenmiddleware.l3p1.xmleditor') 
} catch (e) { 
    console.log(e); 
    return; 
} 

所捕獲的錯誤是

{ 
    message : "Error connecting to native app: com.centeropenmiddleware.l3p1.xmleditor", 
    stack : "Error: Error connecting to native app: com.centeropenmiddleware.l3p1.xmleditor 
      at Object.<anonymous> (extensions::runtime:189:11) 
      at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14) 
      at Object.handleRequest (extensions::binding:55:27) 
      at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14) 
      at Object.<anonymous> (extensions::binding:318:32) 
      at saveas (chrome-extension://plfnjepfbddljeogeacemcpceiofapnm/js/editorRoutines.js:104:35) 
      at HTMLButtonElement.sendFileContentFromEditor (chrome-extension://plfnjepfbddljeogeacemcpceiofapnm/js/editorRoutines.js:89:27)" 
} 

要啓動我已經使用這些命令的應用

google-chrome --load-and-launch-app=/home/paco2/Projects/UPM/XMLEditor/XMLEditor/ --native-messaging-hosts="com.centeropenmiddleware.l3p1.xmleditor.json=/etc/opt/chrome/native-messaing-hosts/com.centeropenmiddleware.l3p1.xmleditor.json" 
google-chrome --load-and-launch-app=/home/paco2/Projects/UPM/XMLEditor/XMLEditor/ 

我使用穩定的谷歌瀏覽器版本(34.0.1847.132)

有什麼不對嗎?

回答

2

Bash是處理Chromium本地消息API的不錯選擇。

它不能處理任何有效的方式二進制信息,請參閱this question

我還是設法雖然推出bash腳本。爲此,我使用了sendNativeMessage。僅發送一次

chrome.runtime.sendNativeMessage('com.centeropenmiddleware.l3p1.xmleditor', 
     {text: "send"}, 
     function(response) {console.log("Received " + 
          chrome.runtime.lastError.message); 
     }); 

它將推出/home/paco2/pp.sh,但分析消息將很難在bash,即使它是一個短send

總之不要使用bash爲此,請嘗試使用C++或Python,並使用subprocess

相關問題