在Firefox WebExtension的后台使用ctypes

在Firefox WebExtension的后台使用ctypes,firefox,jsctypes,firefox-addon-webextensions,Firefox,Jsctypes,Firefox Addon Webextensions,我正在尝试为Firefox编写WebExtension。基本上,我需要一个如何从Firefox运行本地程序的工作示例 我目前实施的扩展包括: background.js content-scripts.js manifest.json 我正在从网页发送一条消息,该消息由content-scripts.js处理,后者将其转发到background.js。但是在background.js中的msgbox函数中,我无法调用ctypes。这给了我一个错误: 未定义ctypes 我尝试以不同的方式加载

我正在尝试为Firefox编写WebExtension。基本上,我需要一个如何从Firefox运行本地程序的工作示例

我目前实施的扩展包括:

  • background.js
  • content-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": "example@mozilla.org",
          "strict_min_version": "45.0"
        }
      },

      "permissions": []
    }



    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();  
    }

background.js:



    {
      "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": "example@mozilla.org",
          "strict_min_version": "45.0"
        }
      },

      "permissions": []
    }



    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();  
    }


您只能使用WebExtension API(在上)在您的WebExtension中。
Cu.import
,尤其是ctypes不是WebExtension API的一部分,因此无法使用。如果您想与操作系统级功能交互,可能需要等待
chrome.runtime.connectNative

如果我尝试通过Firefox的“旧”附加组件实现浏览器操作系统通信(不是通过WebExtension),是否可以在那里使用ctypes?是的,XUL(旧版)和附加SDK扩展都支持ctypes。