Domanda

I'm trying to use Firefox Add-on SDK with js-ctypes to access the methods of a local DLL, but it isn't working.

The main.js code:

var data = require("sdk/self").data;
var pageMod = require("sdk/page-mod");
pageMod.PageMod({
  include: "mysite.com",
  contentScriptFile: data.url("myjs.js")
});

The myjs.js code is just:

Components.utils.import("resource://gre/modules/ctypes.jsm");
alert("hello world");

On Firefox's console I got those messages:

The Components object is deprecated. It will soon be removed.
TypeError: Components.utils is undefined

No "hello world" alert is fired.

What's the problem? Thanks!

È stato utile?

Soluzione

You cannot use js-ctypes from a content script - content scripts have no privileges. You have to do that in the extension itself, via chrome authority:

var {Cu} = require("chrome");
var {ctypes} = Cu.import("resource://gre/modules/ctypes.jsm", null);
var lib = ctypes.open(...);

Altri suggerimenti

This isn't a ctypes issue.

You can't alert from that context. alert is a method on window. So you can do one of two things:

  1. Get the most recent window and run alert there.

    Components.utils.import('resource://gre/modules/Services.jsm');
    Services.wm.getMostRecentWindow(null).alert('hello world');
    
  2. Use prompts service: example at mdn

    Components.utils.import('resource://gre/modules/Services.jsm');
    Services.prompts.alert(null, 'Hello World TITLE', 'hello world message');
    

Also if you are using addon sdk you dont have access to Components so you can't do Components.utils.import you will have to put at top of your main.js this const {Cu} = require('chrome'); then you can do Cu.import('blah')

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top