使用Firefox的插件-SDK我下一個有關創建可重用模塊,該示例使用geolocation API built into Firefox教程,所以代碼很簡單:無法導入nsIDOMGeoGeolocation XPCOM接口
function getCurrentPosition(callback){
var xpcomGeolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsIDOMGeoGeolocation);
xpcomGeolocation.getCurrentPosition(callback);
}
var widget = require("sdk/widget").Widget({
id: "whereami",
label: "Where Am I?",
contentURL: "http://www.mozilla.org/favicon.ico",
onClick: function(){
console.log("clicked!");
getCurrentPosition(function(position){
console.log("latitude: " + position.coords.latitude);
console.log("longitude: " + position.coords.longitude);
});
}
});
運行時火狐插件上,單擊控件給這個錯誤:
Message: [Exception... "Component returned failure code: 0x80570018 (NS_ERROR_XPC_BAD_IID) [nsIJSCID.getService]" nsresult: "0x80570018 (NS_ERROR_XPC_BAD_IID)" location: "JS frame :: resource://gre/modules/XPIProvider.jsm -> jar:file:///tmp/tmpTFowYc.mozrunner/extensions/[email protected]!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-libifbk6zvwaiq-at-jetpack/whereami/lib/main.js :: getCurrentPosition :: line 7" data: no]
第三方模塊的SDK不提供API與navigator.geolocation訪問地理位置。我嘗試使用nsISupports併發生另一個錯誤。我想要做的就是完成教程。 – elaich
我編輯了我以前的答案,以解釋如何訪問導航器對象。希望能幫助到你。 – matagus
謝謝我很肯定,在內容頁面中注入腳本會使用'navigator'工作,但我想訪問chrome服務,並且不知道爲什麼我不能,以及如果我做錯了。 – elaich