我正在创建一个javascript xpcom组件,其来源如下 -
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
function callback() { }
callback.prototype = {
classDescription: "My Hello World Javascript XPCOM Component",
classID: Components.ID("{3459D788-D284-4ef0-8AFF-96CBAF51BD35}"),
contractID: "@jscallback.p2psearch.com/f2f;1",
QueryInterface: XPCOMUtils.generateQI([Components.interfaces.ICallback]),
received:function(data){
alert("Received: " +data);
},
status:function(data){
alert("Status: "+data);
},
expr:function(data){
alert("Expr: "+expr);
}
};
var components = [callback];
function NSGetModule(compMgr, fileSpec) {
return XPCOMUtils.generateModule(components);
}
当我使用以下方式调用它时:
try {
const p2pjcid = "@jscallback.p2psearch.com/f2f;1";
var jobj = Components.classes[p2pjcid].createInstance();
jobj = jobj.QueryInterface(Components.interfaces.ICallback);
jobj.received("Hello from javascript")
}
catch (err) {
alert(err);
return;
}
我得到错误:
[Exception... "'[JavaScript Error: "alert is not defined" {file: "file:///C:/Documents%20and%20Settings/mypc/Application%20Data/Mozilla/Firefox/Profiles/ngi5btaf.default/extensions/spsarolkar@gmail/components/callback.js" line: 11}]' when calling method: [ICallback::received]" nsresult: "0x80570021 (NS_ERROR_XPC_JAVASCRIPT_ERROR_WITH_DETAILS)" location: "JS frame :: chrome://sample/content/clock.js :: initClock :: line 28" data: yes]
答案 0 :(得分:3)
这是因为在XPCOM代码中定义了alert
(尽管您可以获得该组件)。在扩展中提醒时不是一个好主意,因为它会阻止用户与他们所在的页面进行交互。使用非模态烤箱通知功能:
const alert = Components.classes['@mozilla.org/alerts-service;1']
.getService(Components.interfaces.nsIAlertsService)
.showAlertNotification;
使用以下语法:alert(icon, title, body)
。
答案 1 :(得分:1)
您也可以使用JavaScript组件中的dump()。