后来我尝试使用Firefox addon来解决。。。。。。。。。。。。
最终add-on的代码如下lib/main.js
var tabs = require("sdk/tabs"); var self = require("sdk/self"); var pageMod = require("sdk/page-mod"); //当前已经打开的tab数 var openTabs = 0; //要监听的URL关键字 var monitorURL = "oschina"; //contentScript在每次页面打开时被执行。下面是往页面中的window注入一个全局变量 //在页面中可以检测这个注入的变量是否存在,如果存在,则该网页成功被add-on注入了js变量。 pageMod.PageMod({ include : new RegExp(".*" + monitorURL + ".*"), // contentScriptFile:[self.data.url("my-script.js")] contentScript : 'unsafeWindow.pageScriptObject.version = "' + self.version + '"' }); //当任意tab的URL地址栏变化时触发(刷新的时候也会触发) tabs.on("ready", function(currentTab) { console.log("Ready: currentTab=", currentTab.url); var openTabs = 0; //遍历所有的tab页面,只找出感兴趣的地址 for (let tab of tabs) { if (tab.url.indexOf(monitorURL) != -1) { openTabs++; // force close newly opened tab if we already open it. if (openTabs == 2) { currentTab.close(); } } } }); //以下是监听tab页的关闭事件。没用上. /* * tabs.on("close", function(tab){ if(tab.url.indexOf(monitorURL) != -1){ * openTabs--; console.log("openTabs=", openTabs); } }); */
页面上检测add-on是否安装的代码如下:
//在页面中定义一个全局变量(add-on在加载后会自动修改这个变量的内容) window.pageScriptObject = {"version" : "0"}; //期望的add on版本。 var requiredVersion = '1.0'; $(document).ready(function(){ var intervalListener = self.setInterval(function() { checkAddOn(); }, 1000); //尝试检测的次数 var times = 0; //实际安装的add-on版本号 var installedVersion; function checkAddOn (){ times++; console.log("checking addon: ", times); installedVersion = window.pageScriptObject.version; if (installedVersion > 0 || times > 3) { window.clearInterval(intervalListener); if(times > 3) { console.log("Tried 3 times, addon not detected.") //go to error page here. alert("Firefox Add-on not detected, click OK to install the latest Add-on."); window.location= "/path/to/my-addon.xpi"; } else { //addon installed. pass. console.log("Addon version:", installedVersion); var comparedResult = versionCompare(installedVersion, requiredVersion); if(comparedResult < 0 ){ alert("Your Add-on is not the latest, click OK to install the latest Add-on."); window.location= "/path/to/my-addon.xpi"; } else { console.log("You have the required addon installed.") } } } } });
教程如下:
https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Installation 关于tabs的API用法如下:
https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/tabs
https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/page-mod
怎么让add on 访问 dom(从Firefox30开始)
https://blog.mozilla.org/addons/2014/04/10/changes-to-unsafewindow-for-the-add-on-sdk/
##如何取得当前Add-on的版本号
http://stackoverflow.com/questions/28369513/how-can-i-get-the-firefox-add-on-version-number
其它:
http://stackoverflow.com/questions/24626156/how-to-access-dom-in-firefox-addon-script
http://stackoverflow.com/questions/24573345/accessing-to-dom-functions-from-firefox-extenstion
https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Modifying_Web_Pages_Based_on_URL
https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials
####firefox add-ons不能直接访问dom(可以),写extensions则没有任何限制:
https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Tutorial
###20150710新增https://github.com/mdn/addon-sdk-content-scripts
###20150727解决bug,,代码在firefox38可用firefox31报错:
https://blog.mozilla.org/addons/2014/04/10/changes-to-unsafewindow-for-the-add-on-sdk/
相关的不兼容的代码:
```js
console.log("Got duplicate tab, alerting error message.")
//duplicates!!!
var worker = currentTab.attach({
// before close, show user some pop-up message.
// note that we must use \\n here to indicate a new line.
contentScript: 'unsafeWindow.document.write("");unsafeWindow.alert("Only on....");self.port.emit("doClose");'
});
worker.port.on("doClose", function(message) {
console.log("I am closing duplicate tab.")
currentTab.close();
})
```
报错信息如下:###cfx的下载地址已经被替换成jpm了
不过这里还能找到下载地址:https://blog.mozilla.org/addons/2014/08/19/announcing-add-on-sdk-1-17/