gpt4 book ai didi

javascript - 从 HTML 页面内的 Javascript 访问 Firefox 扩展 XPCOM 对象

转载 作者:行者123 更新时间:2023-11-30 13:09:56 26 4
gpt4 key购买 nike

我试图让最基本的 XPCOM javascript 对象可供我加载到我的网页中的 javascript 访问。我正在使用本教程中的示例代码: https://developer.mozilla.org/en-US/docs/How_to_Build_an_XPCOM_Component_in_Javascript

这是我的设置:


安装.rdf:

<?xml version="1.0"?>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:em="http://www.mozilla.org/2004/em-rdf#">

<Description about="urn:mozilla:install-manifest">
<em:id>helloworld@thellamatesting.com</em:id>
<em:name>Hello World</em:name>
<em:version>1.0</em:version>
<em:type>2</em:type>
<em:creator>The Llama</em:creator>
<em:description>Testing</em:description>

<em:targetApplication>
<Description>
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
<em:minVersion>2.0</em:minVersion>
<em:maxVersion>20.0</em:maxVersion>
</Description>
</em:targetApplication>
</Description>
</RDF>



chrome.manifest

content     helloworld    chrome/content/
content helloworld chrome/content/ contentaccessible=yes
overlay chrome://browser/content/browser.xul chrome://helloworld/content/browser.xul

component {4762b5c0-5b32-11e2-bcfd-0800200c9a66} components/HelloWorld.js
contract @thellamatesting.com/helloworld;1 {4762b5c0-5b32-11e2-bcfd-0800200c9a66}

locale helloworld en-US locale/en-US/



组件/HelloWorld.js

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

function HelloWorld() {
// If you only need to access your component from Javascript, uncomment the following line:
this.wrappedJSObject = this;
}

HelloWorld.prototype = {
classDescription: "My Hello World Javascript XPCOM Component",
classID: Components.ID("{4762b5c0-5b32-11e2-bcfd-0800200c9a66}"),
//Also tried
//classID: Components.ID("4762b5c0-5b32-11e2-bcfd-0800200c9a66"),
contractID: "@thellamatesting.com/helloworld;1",
QueryInterface: XPCOMUtils.generateQI(),
// Also tried
//QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIHelloWorld]),
hello: function() {
return "Hello World!";
}
};

var components = [HelloWorld];
if ("generateNSGetFactory" in XPCOMUtils)
var NSGetFactory = XPCOMUtils.generateNSGetFactory(components); // Firefox 4.0 and higher
else
var NSGetModule = XPCOMUtils.generateNSGetModule(components); // Firefox 3.x



测试 HTML:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="application/javascript">

function go() {
try {
var coms = Components;
alert(Components.classes);
var myComponent = Components.classes['@thellamatesting.com/helloworld;1'].getService().wrappedJSObject;
alert(myComponent.hello());
} catch (anError) {
dump("ERROR: " + anError);
}
};

</script>
</head>
<body>

<button onclick="javascript:go()">Click to go</button>

</body>
</html>

在所有这一切之后,我最终得到“Components.classes is undefined”。有谁知道我在这里做错了什么?

非常感谢!

最佳答案

为了从 javascript 上下文中访问 Components 对象,您需要具有扩展功能,即从 chrome:// URL 运行。 曾经是常规网页(由 http://提供)请求扩展功能(称为 UniversalXPConnect)的一种方式,但出于安全考虑已被删除.

我认为您应该多告诉我们您要实现的目标。如果您尝试将插件中的数据导出到网页中,AddonSDK(请参阅 https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/)有一个非常好的协议(protocol),称为 page-mod;它允许您将数据注入(inject)网页。

关于javascript - 从 HTML 页面内的 Javascript 访问 Firefox 扩展 XPCOM 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14260516/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com