1

我试图让我加载到我的网页中的 javascript 可以访问最基本的 XPCOM 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 未定义”。有谁知道我在这里做错了什么?

非常感谢!

4

2 回答 2

2

为了从 javascript 上下文中访问Components对象,您需要具有扩展功能,即从chrome://URL 运行。过去有一种方法可以让常规网页(从 http:// 提供服务)请求扩展功能(称为UniversalXPConnect),但出于安全考虑已将其删除。

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

于 2013-01-10T20:07:30.413 回答
1

感谢乔纳森的建议,我能够想出一个很好的解决方案来解决这个问题。这是我正在使用的代码:

主.js:

var data = require("self").data;
var pageMod = require("page-mod");
const {Cc,Ci} = require("chrome");

pageMod.PageMod({
    include: "*",
    contentScriptFile: data.url("copy-helper.js"),
    onAttach: function(worker) {
        worker.port.on("handleCopy", function(copyInfo) {

            var gClipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper);
            gClipboardHelper.copyString(copyInfo.dataToCopy);
        });
    }
});

复制助手.js:

window.addEventListener("copyEvent", function (event) {

    self.port.emit('handleCopy', event.detail.copyInfo);

}, false);

在我的应用程序中

var event = new CustomEvent("copyEvent", {
    detail:{
        copyInfo: {dataToCopy:"my string"}
    }
});
window.dispatchEvent(event);

希望这对遇到此问题的其他人有所帮助!

于 2013-01-11T21:42:06.540 回答