Firefox扩展无法正常工作

Bru*_*uno 4 html javascript firefox firefox-addon

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

模块层次结构

my_firefox_extension

  • chrome.manifest用于
  • 的install.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
Run Code Online (Sandbox Code Playgroud)

的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>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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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);
});
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Bru*_*oLM 6

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

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
);
Run Code Online (Sandbox Code Playgroud)