在Firefox附加组件中,如何实现将脚本注入HTML页面并将消息发送回主脚本?

时间:2014-03-25 15:12:20

标签: javascript firefox firefox-addon

我的附加组件专门适用于单个网站。

它会注入一个内容脚本并向弹出窗口(firefox术语中的面板)发送一条消息进行显示。

这是我的内容:

//this code is same for firefox as well as chrome since it is that which manipulates the page in which it is injected, so I think this is indepent of browser

var table = document.getElementsByClassName("collapse")[0];

var marks = new Array();

 for(var i=0;i<8;i++)
   marks[i] = table.children[0].children[i+1].children[5].innerHTML;

 var total=0;
 for(var i=0;i<8;i++)
   total += Number(marks[i]);

 ------------------------------------------------
//this code is different for firefox
 //code to send the data to popup 
 var fromDOM = total;
chrome.runtime.sendMessage(fromDOM);//what is the equivalent method for sending a message to popup(panel)?

popup.html

//no change , I think would be same with firefox 

<!DOCTYPE HTML>

<html>
  <head>
  <title>Popup page</title>
   <link rel="stylesheet" type="text/css" href="popup.css">
  </head>
  <body>
<div id="output">
  <h1><form name="F1" method="POST">
         TOTAL: <p id="total"></p>
    PERCENTAGE: <p id="percentage"></p>
  </form></h1>
</div>  

  <script src="popup.js">

  </script>

  </body>
</html>

popup.js:

//code to load the contentscript into the page 
chrome.tabs.executeScript({"file": "contentscript.js"});    
//equivalent firefox code??***
-----------------------------------------------------------------
    var total= 0,percentage = 0;

chrome.runtime.onMessage.addListener(function(response){
total = response;***
//Getting response sent from contentscript, how can I do the same in firefox?
percentage = total/7.25; 


document.getElementById("total").innerHTML = total;
document.getElementById("percentage").innerHTML = percentage.toFixed(2)+" %";


});

提到的某些chrome API方法的具体firefox方法是什么?

如果有人能帮助我,我会很高兴的。 谢谢

0 个答案:

没有答案