简体   繁体   English

从Firefox中的网页访问扩展程序的功能

[英]Access extension's functions from webpage in Firefox


I'm trying to write an extension to Firefox that would allow me to access internal functions/classes/objects of it from my webpage. 我正在尝试为Firefox编写扩展程序,以便允许我从网页访问其内部功能/类/对象。 I want them to be visible and accesible in DOM. 我希望它们在DOM中可见且可访问。 It worked when extension was loaded as a component from chrome.manifest file but it doesn't seem that it's possible in e10s (multiprocess Firefox) anymore. 当从chrome.manifest文件中将扩展作为组件加载时,它可以工作,但似乎在e10s(多进程Firefox)中不再可行。
So I was trying and trying and the best option I have found so far seems to be using exportFunction, createObjectIn and cloneInto functions. 所以我一直在尝试,到目前为止,我发现的最佳选择似乎正在使用exportFunction,createObjectIn和cloneInto函数。 They work fine when expected to make objects visible from pages loaded by the extension itself but not from remote ones. 当期望使对象从扩展本身加载的页面可见,而对远程页面不可见时,它们可以正常工作。
I'm using Addon-SDK now and my code is 我现在正在使用Addon-SDK,我的代码是

And then 接着

function injectTest(event) {
    let domWindow = event.subject;

//This creates and object that is always visible but never accesible from page not loaded by the extension     
foo = Cu.createObjectIn(domWindow.wrappedJSObject, {defineAs: "testSDK"});     
//This exports my function fine but I can export it only into an existing object
//That's why I'm using "crypto" here
Cu.exportFunction(test.bind(this, domWindow),
                    domWindow.crypto.wrappedJSObject,
                    { defineAs: "test" });


//This exports my function to my object but only on pages loaded by the extension    
Cu.exportFunction(test.bind(this, domWindow),
                    foo,
                    { defineAs: "test2" });

//Same here, cloned_var seems to be not accesible from remote webpage
var to_be_cloned = {"greet" : "hey"};
    foo.cloned_var = Cu.cloneInto(to_be_cloned, foo);
}



  exports.main = function(options, callbacks) {
      if (!gInitialized &&
          (options.loadReason == "startup" ||
           options.loadReason == "install" ||
           options.loadReason == "enable")) {
        log("initializing - " +  options.loadReason);
        try {
          events.on("content-document-global-created", injectTest);
        } catch (error) {
          log(error);
        }
        gInitialized = true;
      }
    };

I'm totally new to javascript and Firefox extensions so I have no idea how to make it work. 我对javascript和Firefox扩展完全陌生,所以我不知道如何使其工作。 What I'm doing wrong? 我做错了什么? Is there any better idea to access extension's objects? 有没有更好的主意来访问扩展的对象?

Thank you in advance for help. 预先感谢您的帮助。

@edit 19.05.15 Tried using page-mod. @edit 19.05.15使用page-mod进行了尝试。 It does work but not as well as I need. 它确实可以工作,但是不如我所需。 main.js file main.js文件

var data = require("sdk/self").data; 
var pageMod = require("sdk/page-mod"); 

pageMod.PageMod({
  include: "mywebsite",
    contentScriptFile: [data.url("cscript.js")],
    contentScript: 'window.alert("Page matches ruleset");'
}); 

cscript.js file (in data folder) cscript.js文件(在数据文件夹中)

var contentScriptObject = {
    "greeting" : "hello from add-on",
    b: 1,

    powitaj: function(){
    return(this.greeting);
    }, //when called from console returns "hello from add-on"


    is_b: function(){
        if(b){
        return true;
        }else{
        return false;
        }
    }, //undefined

    is_thisb: function(){
        if(this.b){
        return true;
        }else{
        return false;
        }
    }, //returns 1

    func: function(){
    console.log("ok")
    }, //returns "ok"

    is_func: function(){
        func();         
    }, //undefined

    is_thisfunc:function(){
        this.func();
    } //undefined

};

So from my website I can access inner variables (actually variables defined globally too and modify them as well), I can access inner functions (not outer - not included in the code) but my inner functions cannot call each other and I'd like to be able to do that. 因此,从我的网站上,我可以访问内部变量(实际上也是全局定义的变量,也可以对其进行修改),我可以访问内部函数(不是外部函数-不包括在代码中),但是内部函数不能互相调用,我想能够做到这一点。

To run chrome-privileged code in a e10s-content process you'll need framescripts 要在e10s内容流程中运行chrome特权代码,您需要使用框架脚本

The SDK has some wrapper modules to simplify the communication and module loading between parent and child process 该SDK有一些包装模块,以简化之间的通信和模块加载的父过程

If you only need exportFunction / cloneInto / createObjectIn then using the page-mod module may be sufficient, if i recall correctly those helpers are made available in its context. 如果只需要exportFunction / cloneInto / createObjectIn那么使用page-mod模块就足够了,如果我正确地记得的话,可以在其上下文中使用这些帮助器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM