通过Javascript控制Firefox扩展 [英] Controlling a Firefox Extension via Javascript
问题描述
是否可以使用javascript来控制覆盖Firefox的扩展?我已经提取了扩展的内容,并确定了我需要运行的功能/方法,但是它们在控制台范围内是不可访问的。
感谢您提出任何建议。
在给定正确的情况下与其他插件进行交互。
我的测试用例在这里是 com.googlecode.sqlitemanager.openInOwnWindow()
,它是 SqliteManager
addon 。
$ b
-
在较新的版本中(我使用Nightly),有浏览器工具箱。使用它就像在控制台中打开一个工具箱并执行
com.googlecode.sqlitemanager.openInOwnWindow()
一样简单。 -
您可以使用浏览器控制台(或任何启用了Chrome的WebDev控制台,例如about:newtab的控制台)。但是你需要一些样板代码来找到浏览器窗口。所以这里是你可以在那里执行的代码:
var bwin = Services.wm.getMostRecentWindow(navigator:browser); bwin.com.googlecode.sqlitemanager.openInOwnWindow()
-
同样,启用Chrome调试。然后打开Scratchpad并在 菜单中切换到 Chrome 。现在在我们的Scratchpad中执行
com.googlecode.sqlitemanager.openInOwnWindow()
就可以了。 当然你也可以编写你自己的overlay插件。 -
作为最后的手段,补丁本身就是附加组件。
-
引导程序/ SDK附件:您可以加载
XPIProvider.jsm
(最近更改了位置)并进入引导范围(运行环境bootstrap.js
)通过XPIProvider.bootstrapScopes [addonID]
,然后从那里拿出来(使用引导范围中的任何东西,例如SDK加载器)
现在介绍正确的情况:如果以及如何与某个加载项取决于附加组件。附加组件可能在覆盖层中有全局符号,因此在浏览器窗口中可能会有全局符号,例如我使用的示例。或者可以使用(在某种程度上)JS代码模块。或者有他们自己的自定义加载器的东西(例如AdBlock Plus有自己的 require()
类似的东西,SDK附加组件有他们自己的加载器,这不是很容易...
因为你的问题是非常不明确的,所以我会把它留在这里。
按问题编辑者提问:这是正确的,但是我想我会添加一个最终使用的代码的示例,实际上是直接从Mozilla开发人员网络站点获取的: p>
在我的chrome js中:
var myExtension = {
myListener:function(evt){
IprPreferences.setFreshIpStatus(true); //替换为任何你希望在扩展中激活的元素
$ b document.addEventListener(MyExtensionEvent,function(e){myExtension.myListener(e );},false,true);
//最后一个值是一个特定于Mozilla的值,用于指示不可信内容被允许触发事件。
在网页内容中:
var element = document.createElement(MyExtensionDataElement);
element.setAttribute(attribute1,foobar);
element.setAttribute(attribute2,hello world);
document.documentElement.appendChild(element);
var evt = document.createEvent(Events);
evt.initEvent(MyExtensionEvent,true,false);
element.dispatchEvent(evt);
Is it possible, using javascript, to control an overlay firefox extension? I've extracted the contents of the extension and have identified what functions/methods I need to run, but they are not accessible within the scope of the console.
Thanks in advance for any ideas.
Yes it possible to interact with other add-ons, given the right circumstances.
My test case here will be com.googlecode.sqlitemanager.openInOwnWindow()
, which is part of the SqliteManager
addon.
In newer builds (I'm using Nightly), there is the Browser Toolbox. With it is is as simple as opening a toolbox and executing
com.googlecode.sqlitemanager.openInOwnWindow()
in the Console.You may instead use the Browser Console (or any chrome enabled WebDev Console for that matter, e.g. the Console of "about:newtab"). But you need some boilerplate code to first find the browser window. So here is the code you can execute there:
var bwin = Services.wm.getMostRecentWindow("navigator:browser"); bwin.com.googlecode.sqlitemanager.openInOwnWindow()
Again, enable chrome debugging. Then open a Scratchpad and switch to Chrome in the Environment menu. Now executing
com.googlecode.sqlitemanager.openInOwnWindow()
in our Scratchpad will work.You may of course write your own overlay add-on.
As a last resort, patch the add-on itself.
Bootstrapped/SDK add-ons: you can load
XPIProvider.jsm
(which changed location recently) and get to the bootstrapped scope (run environment ofbootstrap.js
) viaXPIProvider.bootstrapScopes[addonID]
, and take it from there (use whatever is in the bootstrap scope, e.g. the SDK loader).
Now about the right circumstances: If and how you can interact with a certain add-on depends on the add-on. Add-ons may have global symbols in their overlay and hence browser window, such as in the example I used. Or may use (to some extend) JS code modules. Or have their own custom loader stuff (e.g. AdBlock Plus has their own require()
-like stuff and SDK add-ons have their own loader, which isn't exactly easy to infiltate)...
Since your question is rather unspecific, I'll leave it at this.
Edit by question asker: This is correct, however I figured I'd add an example of the code I ended up using in the end, which was in fact taken directly from mozilla's developer network website:
In my chrome js:
var myExtension = {
myListener: function(evt) {
IprPreferences.setFreshIpStatus(true); // replace with whatever you want to 'fire' in the extension
}
}
document.addEventListener("MyExtensionEvent", function(e) { myExtension.myListener(e); }, false, true);
// The last value is a Mozilla-specific value to indicate untrusted content is allowed to trigger the event.
In the web content:
var element = document.createElement("MyExtensionDataElement");
element.setAttribute("attribute1", "foobar");
element.setAttribute("attribute2", "hello world");
document.documentElement.appendChild(element);
var evt = document.createEvent("Events");
evt.initEvent("MyExtensionEvent", true, false);
element.dispatchEvent(evt);
这篇关于通过Javascript控制Firefox扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!