【问题标题】:My add-on, compiled from a Greasemonkey script that uses GM_ functions, does not work in Firefox version 31+我的插件是从使用 GM_ 函数的 Greasemonkey 脚本编译的,在 Firefox 31+ 版本中不起作用
【发布时间】:2014-11-03 09:37:37
【问题描述】:

我有一个 Firefox 附加组件,它是一个基于覆盖的扩展,可在 Firefox 版本高达 31 中运行。但它不适用于 32 及更高版本。我通常使用Userscript Compiler 工具来构建我的扩展。我在扩展程序的控制台中看不到任何内容(错误/输出)。

虽然当我使用 Greasemonkey 插件运行我的脚本时,它工作正常。问题是在将扩展构建为 XPI 文件后运行扩展。

下面是我的扩展结构:

myextension
 |-->skin
    |-->classic
 |-->content
    |-->myscript.user.js
    |-->myscriptPrefman.js
    |-->myscriptScript-compiler.js
    |-->myscriptXmlhttprequester.js
    |-->script-compiler-overlay.xul
 |-->chrome
 |-->install.rdf
 |-->icon.png
 |-->chrome.manifest

下面是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>{9ef9b86c-03da-4e10-9552-e97a6d258af5}</em:id>
<em:name>Sample Extension</em:name>
<em:version>2.6</em:version>
<em:description>A sample extension - Firefox</em:description>
<em:creator>Mozdev</em:creator>
<em:contributor>Greasemonkey Compiler by Anthony Lieuallen;</em:contributor>
<em:contributor>http://arantius.com/</em:contributor>
<em:homepageURL>www.example.com</em:homepageURL>
<em:targetApplication><Description>
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
<em:minVersion>2.0</em:minVersion>
<em:maxVersion>35.*</em:maxVersion>
</Description>
</em:targetApplication>
</Description>
</RDF>

chrome.manifest内容:

content myextension content/
overlay chrome://browser/content/browser.xul   chrome://myextension/content/script-compiler-overlay.xul
skin myextension classic/1.0 skin/classic/

我是否需要更改扩展中的任何内容或将其转换为 Bootstrap 扩展或使用附加 SDK 来构建 XPI 文件?

更新:

我已关注the Add-on SDK method suggested by Wladimir 并创建了一个 XPI 安装程序文件。但是用户脚本没有在页面内运行。
代码仍然从 Greasemonkey 插件运行。

This is the Greasemonkey script 在 Greasemonkey 中运行良好,但在通过编译器和 SDK 进行扩展时就不行了。

我在我的本地机器上使用了 steps to install 附加 SDK。我使用cfx 命令构建XPI 文件。我在 Firefox 最新版本(ver.33.0.2)中安装它并访问 openuserjs.org 我什么也看不到。这是为什么呢?

【问题讨论】:

  • @BrockAdams 我不希望我的扩展程序发布在 AMO 或 userscripts.org 上……而且 userscrits.org 已经关闭……所以我无法尝试在您的答案中找到解决方案您标记为重复的其他问题..
  • 然后使用第二个答案(Wladimir's),它显示了使用 addon-SDK 将您的 GM 脚本转换为扩展的关键部分。
  • @BrockAdams 我已经通过使用 Addon-SDK 构建插件尝试了另一种解决方案,但它不起作用..我使用 pageMod api 来修改准备好的页面..你可以看看有什么问题。并重新提出问题?
  • 好的,问题现在已经足够不同了。除非您可以让 Arantius 对其进行更新,否则编译器是无用的。要使用 SDK 方法,您还需要为您使用的 GM_ 函数提供填充程序。

标签: firefox-addon greasemonkey xpi


【解决方案1】:

我已经复制了代码,只是使用 Firefox Request Api for GM_XmlhttpRequest 和本地存储来在本地存储信息。并使用 Restartless Firefox Addon SDK Api 的主干 Message Passing 使脚本与 main.js 通信。

var data = require("self").data;
var pageMod = require("page-mod");
pageMod.PageMod({
  include: "*.example.com", //mywebsite url
  contentScriptWhen: 'start',
  contentScriptFile: data.url("contentScript.js")
});

contentScriptFile 是您想要编写用户脚本并使用最新 Firefox AddonSDK API 中的端口消息与 main.js 通信的地方。

我使用Request API 从服务器获取信息,而不是使用不受支持的 GM_xmlhttpRequest。

var Request = require("sdk/request").Request;
var userDetails = Request({
  url: "http://example.com/user/1",
  onComplete: function (response) {
    var res = response.json[0];
  }
});

现在扩展程序无需重新启动,并且使用 Firefox 文档中的高级和低级 api 完全重写。希望未来的浏览器更改将使用这些 api 并在更长的时间内支持扩展。

【讨论】:

    猜你喜欢
    • 2012-04-25
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    相关资源
    最近更新 更多