我花了几天时间阅读各种教程、MDN 条目和SO 帖子,我开始怀疑我遗漏了一些明显的东西,但我对 XPCOM 太缺乏经验,无法发现它。我大约 80% 确定我的自定义组件(components/fooLogin.js)中的某个地方存在错误。
问题:当加载项初始化时(当我从 chrome/content/foologin.js 调用 loadData() 时),我收到一条错误消息:
TypeError: Components.classes['@foo.com/foologinautocomplete;1'] 未定义
我是否可能在注册类之前尝试创建组件?我还需要做些什么来注册它吗?任何提示将不胜感激。
相关代码:(如果需要,很乐意提供任何其他代码)
组件/fooLogin.js:
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
function fooLoginAutoComplete(){
this.wrappedJSObject = this;
}
fooLoginAutoComplete.prototype = {
classID: Components.ID("loginac@foo.com"),
contractID: "@foo.com/foologinautocomplete;1",
classDescription: "Auto complete for foo",
QueryInterface: XPCOMUtils.generateQI([]),
complete: function(str){ // Autocomplete functionality will in this function
return null;
}
};
var NSGetFactory = XPCOMUtils.generateNSGetFactory([fooLoginAutoComplete]);
铬/内容/foologin.js:
let fooLogin = {
dataLoaded : false,
searchFilter = null,
...
loadData : function(){
...
try{
alert(1); // This alert fires
this.searchFilter = Components.classes['@foo.com/foologinautocomplete;1']
.getService().wrappedJSObject;
alert(2); // I get the error before this alert
}catch(e){alert(e);}
this.dataLoaded = true;
}
}
window.addEventListener("load", function(){
if(!fooLogin.dataLoaded) fooLogin.loadData();
}
chrome.manifest:
content foologin chrome/content/
content foologin chrome/content/ contentaccessible=yes
skin foologin classic chrome/skin/
locale foologin en-US chrome/locale/en-US/
component loginac@foo.com components/fooLogin.js
contract @foo.com/foologinautocomplete;1 loginac@foo.com
overlay chrome://browser/content/browser.xul chrome://foologin/content/foologin.xul