我正在嘗試爲Firefox編寫WebExtension。低調地說,我需要一個工作示例來說明如何從Firefox運行本地程序。在Firefox的後臺使用ctypes WebExtension
我當前實現擴展包括:
- background.js
- 內容scripts.js中
- manifest.json的
從網頁我送其處理的消息由content-scripts.js轉發給background.js。但是在background.js的msgbox函數中,我無法調用ctypes。它給我的錯誤:
ctypes的是沒有定義
我試圖加載的ctypes不同的方式,但它不工作: Components.utils.import("resource://gre/modules/ctypes.jsm")
或 var {ctypes} = Cu.import("resource://gre/modules/ctypes.jsm"
我做什麼了?
這是我的擴展的源代碼。
manifest.josn:
{
"description": "Test web-extension.",
"manifest_version": 2,
"name": "Example",
"version": "1.0",
"homepage_url": "http://example.org",
"icons": {
"48": "icons/example-48.png"
},
"content_scripts": [
{
"matches": ["*://web.localhost.com/*"],
"js": ["content-scripts.js"]
}
],
"background": {
"scripts": ["background.js"]
},
"applications": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "45.0"
}
},
"permissions": []
}
background.js:
chrome.runtime.onMessage.addListener(msgbox());
function msgbox() {
var lib = ctypes.open("C:\\WINDOWS\\system32\\user32.dll");
/* Declare the signature of the function we are going to call */
var msgBox = lib.declare("MessageBoxW",
ctypes.winapi_abi,
ctypes.int32_t,
ctypes.int32_t,
ctypes.jschar.ptr,
ctypes.jschar.ptr,
ctypes.int32_t);
var MB_OK = 0;
var ret = msgBox(0, "Hello world", "title", MB_OK);
lib.close();
}
如果我將嘗試通過Firefox的「舊」附件(不通過WebExtension)實現瀏覽器-OS通信,是否可以在那裏使用ctypes? – Denver
是的,XUL(舊版)和Add-on SDK擴展都支持ctypes。 – evilpie
謝謝@ evilpie! – Denver