簡體   English   中英

從HTML頁面中的Javascript訪問Firefox擴展XPCOM對象

[英]Access Firefox extension XPCOM object from Javascript inside an HTML page

我正在嘗試將最基本的XPCOM javascript對象添加到我加載到我的網頁的javascript中。 我正在使用本教程中的示例代碼: https//developer.mozilla.org/en-US/docs/How_to_Build_an_XPCOM_Component_in_Javascript

這是我的設置:


install.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未定義”。 有誰知道我在這里做錯了什么?

非常感謝!

為了從javascript上下文訪問Components對象,您需要具有擴展功能,即從chrome:// URL運行。 曾經有一個常規的網頁的方式(從http服務://)要求擴展功能(稱為UniversalXPConnect ),但它已被刪除出於安全考慮。

我想你應該多告訴我們你想要實現的目標。 如果您嘗試將插件中的數據導出到網頁中,AddonSDK(請參閱https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/ )非常好這樣做的協議稱為page-mod; 它允許您將數據注入網頁。

感謝Jonathan的建議,我能夠為這個問題找到一個很好的解決方案。 這是我正在使用的代碼:

main.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);
        });
    }
});

復制helper.js:

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

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

}, false);

在我的應用程序JavaScript

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

希望這可以幫助其他遇到此問題的人!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM