firefoxjsctypesfirefox-addon-webextensions

Using ctypes in background in Firefox WebExtension


I am trying to write WebExtension for Firefox. Bassically, I need a working example how to run local programs from Firefox.

My current implementation of extension consist of:

From webpage I am sending a message which handled by content-scripts.js who is forwarding it to background.js. But in msgbox function in background.js I unable to call ctypes. It gives me error:

ctypes is not defined

I tried to load ctypes different ways but it doesn't work: Components.utils.import("resource://gre/modules/ctypes.jsm") or var {ctypes} = Cu.import("resource://gre/modules/ctypes.jsm"

What I do wrong?

Here is the source code of my extension.

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": []
    }

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


Solution

  • You can only use WebExtension APIs (on MDN) in your WebExtension. Cu.import and especially ctypes are not part of the WebExtension API, and so can't be used. You will probably have to wait for chrome.runtime.connectNative if you want to interact with OS level functions.