2014-03-03 56 views
2

我想用js-ctypes的Firefox附加组件来访问本地DLL的方法,但它不工作。Firefox附加SDK和js-ctypes

的main.js代码:

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

的myjs.js代码只是:

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

在Firefox的控制台,我得到了这些消息:

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

否“你好世界“警报被解雇。

有什么问题?谢谢!

回答

4

你不能使用js-ctypes从一个小说nt脚本 - 内容脚本没有特权。你所要做的是,在扩展本身,通过chrome authority

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

你的答案让我走上正轨。我也不能在内容脚本中“要求”。我必须在main.js中使用ctypes并使脚本进行通信。更多信息:https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts/Communicating_With_Other_Scripts –

1

这不是一个ctypes问题。

您无法从该上下文中发出警报。警报是窗口上的一种方法。所以你可以做两件事之一:

  1. 获取最近的窗口并在那里运行警报。

    Components.utils.import('resource://gre/modules/Services.jsm'); 
    Services.wm.getMostRecentWindow(null).alert('hello world'); 
    
  2. 使用提示服务:example at mdn

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

此外,如果你正在使用插件SDK你没有访问Components所以你不能做Components.utils.import你将不得不把在顶部你的main.js这const {Cu} = require('chrome');那么你可以做Cu.import('blah')

+0

一个内容脚本可以很好地使用'警报()',它运行在一个窗口的背景下 - 在这里这不是问题。 –

+0

啊我学会了osmething :) Thx男人! :) – Noitidart

相关问题