简体   繁体   English

在firefox插件中调用javascript类函数

[英]Invoke javascript class function in firefox addon

I needed xpaths generated by Firepath (Firebug extension), to be passed to my native JavaScript class object present in DOM. 我需要将Firepath (Firebug扩展)生成的Firepath传递给DOM中存在的本机JavaScript类对象。 So, I am modifying Firepath extension itself, now to pass the generated xpath to my JavaScript class function present in DOM, I can't figure out a way. 因此,我正在修改Firepath扩展本身,现在将生成的xpath传递给DOM中存在的我的JavaScript类函数,我想不出办法。 I tried many solutions like inside the extension function, the following example works: 我尝试了很多解决方案,例如在扩展功能内部,以下示例有效:

window.alert("hello");

But the following doesn't: 但是以下不是:

    var pObj =  new window.wrappedJSObject.PClass();
    alert(pObj);
    pObj.CalledFromAddOn();

Any help will be highly appreciated. 任何帮助将不胜感激。

After doing some hard work I finally got it working, the document and window objects in Firefox extension refer to different document and window objects and not the DOM (should be obvious), so we need to find the current window to execute the function or class function, whatever. 经过一些辛苦的工作,我终于使它工作了,Firefox扩展中的文档和窗口对象引用了不同的文档和窗口对象,而不是DOM(应该很明显),所以我们需要找到当前窗口来执行函数或类。功能,随便。 So, here is the code snippet, which you can use in your extension to invoke DOM javascript: 因此,这是代码段,您可以在扩展程序中使用该代码段来调用DOM JavaScript:

var doc = Application.activeWindow.activeTab.document;   
var win = doc.defaultView;  // will give you the DOM window object atleast on firefox and chrome

// Now call your functions or create objects

win.wrappedJSObject.hello();
var pToolObj =  new win.wrappedJSObject.PTool();
alert(pToolObj.currTaskNo);

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

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