【问题标题】:Firefox extension not workingFirefox 扩展不起作用
【发布时间】:2011-08-18 10:02:36
【问题描述】:

我已经创建了一个 Firefox 扩展,但我无法使用它(没有任何反应)。 有人知道为什么吗?

模块层次结构

my_firefox_extension

  • chrome.manifest
  • 安装.rdf
  • 镀铬/
    • 内容/
      • locale.html
      • overlay.js
      • sample.xul

代码

chrome.manifest

content   firefox_extension chrome/content/

overlay chrome://browser/content/browser.xul  chrome://firefox_extension/content/sample.xul

安装.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>displaypages@bruno.com</em:id>
    <em:name>Display the page locale</em:name>
    <em:description>Welcome to this extension that displays the page locale when a user opens a new tab or windows</em:description>
    <em:version>0.1</em:version>
    <em:creator>Bruno Da Silva</em:creator>
    <em:homepageURL>https://www.example.com</em:homepageURL>
    <em:type>2</em:type>

    <!-- Mozilla Firefox -->
    <em:targetApplication>
    <Description>
       <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
       <em:minVersion>3.0</em:minVersion>
       <em:maxVersion>4.0.*</em:maxVersion>
    </Description>
       </em:targetApplication>
  </Description>
</RDF>

sample.xul

<?xml version="1.0"?>

<overlay id="firefox_extension-browser-overlay"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <script type="application/x-javascript" src="chrome://firefox_extension/content/overlay.js"/>

</overlay>

overlay.js

function Read(file)
{
     var ioService=Components.classes["@mozilla.org/network/io-service;1"]
                             .getService(Components.interfaces.nsIIOService);
     var scriptableStream=Components
         .classes["@mozilla.org/scriptableinputstream;1"]
         .getService(Components.interfaces.nsIScriptableInputStream);

     var channel=ioService.newChannel(file,null,null);
     var input=channel.open();
     scriptableStream.init(input);
     var str=scriptableStream.read(input.available());
     scriptableStream.close();
     input.close();
     return str;
 }

gBrowser.addEventListener("DOMContentLoaded", function(e) {
    var documentElement = e.originalTarget.defaultView.document;
    var div = documentElement.createElement("div");
    div.innerHTML = Read("chrome://firefox_extension/content/locale.html");
    documentElement.body.appendChild(div);
});

locale.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
   <head>
       <title>Page displayed when a user opens a new tab or window</title>
       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
   </head>
   <body>
         <p>Some text<p>
   </body>
</html>

【问题讨论】:

  • 什么时候出现错误?如果是在安装过程中,请在 about:config 中设置首选项 extensions.logging.enabled,然后重试,并复制错误控制台中看到的错误。
  • @Matthew Wilson 在安装过程中。我更新了我的帖子,将收到的消息放入控制台。
  • 如果你在地址栏输入chrome://firefox_extension/content/overlay.js会发生什么?
  • 可能是无效的目标版本&lt;em:maxVersion&gt;6.0a1&lt;/em:maxVersion&gt;。请改用&lt;em:maxVersion&gt;4.0.*&lt;/em:maxVersion&gt;
  • 你能把 XPI 文件贴在某个地方吗?

标签: javascript html firefox firefox-addon


【解决方案1】:

您错过了一个可能导致错误的参数:

gBrowser.addEventListener("DOMContentLoaded", function(e) {
    var documentElement = e.originalTarget.defaultView.document;
    var div = documentElement.createElement("div");
    div.innerHTML = Read("chrome://firefox_extension/content/locale.html");
    documentElement.body.appendChild(div);
},

false // missing parameter on addEventListener
      // add this and it might work
);

【讨论】:

  • 啊——好眼睛!没有任何线索——你必须翻阅别人的代码。有 16.8k 的积分,答案和时间是一种慈善行为,而不是积分行为!
【解决方案2】:

(只是猜测,不涉及测试)

NS_ERROR_FILE_TARGET_DOES_NOT_EXIST 可能是由于 sample.xul 中的脚本文件引用不正确

<script type="application/x-javascript" src="chrome//firefox_extension/content/overlay.js"/>

src 属性在“chrome”之后缺少一个冒号。应该是

src="chrome://firefox_extension/content/overlay.js"

【讨论】:

  • 感谢您的回答。我已经尝试过了,但它不起作用(我收到与以前相同的消息错误)。
【解决方案3】:

Firefox 中的文件可以更正。

试试下面的

完全退出 Firefox,然后打开您的 Firefox 配置文件夹并删除或重命名这些文件:

extensions.ini extensions.cache 扩展名.rdf

从 Firefox 4 开始,同样删除或重命名:

extensions.sqlite extensions.sqlite-journal(如果找到)

注意:虽然上述文件可以删除,但重命名它们(例如,“extensionsOLD.ini”、“extensionsOLD.cache”等)通常被认为是更安全的选择。这实现了相同的结果,但允许用户稍后从他们那里检索任何可能需要的信息。

然后尝试重新启动浏览器并安装它们

否则还有另一种方法可能有效(但不知道它们为什么/如何工作)

启用第三方 cookie - 转到工具 -> 选项 -> 隐私并选中接受第三方 cookie 框。

【讨论】:

  • 尝试在 firefox 配置文件中将 extensions.checkCompatibility 属性设置为 false。无论如何,您能否发布它在安装时显示的确切错误
  • 在标签警告中,我得到了我在帖子中发布的确切两个警告。感谢您的其他建议,我会尝试一下。
【解决方案4】:

live development environment 中使用它时,您的扩展是否有效,而不是 xpi 安装程序出现问题?

在 Firefox 关闭的情况下,创建一个 同名的“指针”文件 您的扩展程序的描述:ID(如 在您的 install.rdf 中找到)在配置文件中 文件夹/扩展/并编辑它,以便 它包含您的路径 扩展的文件夹(根 包含包含 install.rdf 和 chrome.manifest 文件)。

例如helloworld 的 ID 是 helloworld@mozilla.doslash.org 和我们 想注册它 X:\Dev\helloworld\(即有 X:\Dev\helloworld\install.rdf 文件 等等。)。只需将一行放入 此路径下的文件:profile 文件夹/扩展/helloworld@mozilla.doslash.org

X:\Dev\helloworld\ - 注释尾随 斜线且没有 CR;这必须是单行

(重新)启动 Firefox,并检查您的 扩展已安装。

这将帮助您在开始解决安装问题之前确保扩展程序正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 2022-10-19
    相关资源
    最近更新 更多