通过 Javascript 控制 Firefox 扩展 [英] Controlling a Firefox Extension via Javascript

查看:23
本文介绍了通过 Javascript 控制 Firefox 扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 javascript 来控制覆盖 Firefox 扩展?我已经提取了扩展的内容并确定了我需要运行的函数/方法,但它们在控制台范围内无法访问.

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.

我的测试用例是 com.googlecode.sqlitemanager.openInOwnWindow(),它是 SqliteManager 插件.

My test case here will be com.googlecode.sqlitemanager.openInOwnWindow(), which is part of the SqliteManager addon.

  1. 在较新的版本中(我使用的是 Nightly),有 浏览器工具箱.使用它就像打开一个工具箱并在控制台中执行 com.googlecode.sqlitemanager.openInOwnWindow() 一样简单.

  1. 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.

您可以改为使用浏览器控制台(或任何支持 Chrome 的 WebDev 控制台,例如about:newtab"的控制台).但是您需要一些样板代码来首先找到浏览器窗口.所以这里是你可以在那里执行的代码: var bwin = Services.wm.getMostRecentWindow("navigator:browser");bwin.com.googlecode.sqlitemanager.openInOwnWindow()

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()

同样,启用 Chrome 调试.然后打开一个 Scratchpad 并在 Environment 菜单中切换到 Chrome.现在在我们的便笺簿中执行 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 附加组件:您可以加载 XPIProvider.jsm(最近更改了位置)并进入引导范围(bootstrap.js 的运行环境代码>) 通过 XPIProvider.bootstrapScopes[addonID],然后从那里获取它(使用引导范围内的任何内容,例如 SDK 加载程序).

Bootstrapped/SDK add-ons: you can load XPIProvider.jsm (which changed location recently) and get to the bootstrapped scope (run environment of bootstrap.js) via XPIProvider.bootstrapScopes[addonID], and take it from there (use whatever is in the bootstrap scope, e.g. the SDK loader).

现在关于正确情况:您是否以及如何与某个附加组件交互取决于附加组件.附加组件可能在其覆盖层和浏览器窗口中有全局符号,例如我使用的示例.或者可以使用(在某种程度上)JS 代码模块.或者有自己的自定义加载器(例如 AdBlock Plus 有自己的 require() 之类的东西,SDK 附加组件有自己的加载器,这并不容易渗透)...

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.

由提问者这是正确的,但是我想我会添加一个我最终使用的代码示例,该示例实际上直接取自 mozilla 的开发者网络网站:

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:

在我的 chrome js 中:

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.

在网页内容中:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆