Explorar el Código

improved badge and popup menu

possible to add option of whether to show scripts run in the top level window or all of them in each tab

Signed-off-by: Gerald <[email protected]>
Gerald hace 11 años
padre
commit
605cd03f8d
Se han modificado 2 ficheros con 31 adiciones y 19 borrados
  1. 19 10
      background.js
  2. 12 9
      injected.js

+ 19 - 10
background.js

@@ -222,6 +222,17 @@ function getMetas(ids,src,callback) {	// for popup menu
 	}
 	getOne();
 }
+var badges={};
+function setBadge(n,src,callback) {
+	var o;
+	if(src.id in badges) o=badges[src.id];
+	else badges[src.id]=o={num:0};
+	o.num+=n;
+	chrome.browserAction.setBadgeBackgroundColor({color:'#808',tabId:src.tab.id});
+	chrome.browserAction.setBadgeText({text:o.num.toString(),tabId:src.tab.id});
+	if(o.timer) clearTimeout(o.timer);
+	o.timer=setTimeout(function(){delete badges[src.id];},300);
+}
 function getCacheB64(ids,src,callback) {
 	var o=db.transaction('cache').objectStore('cache'),data={};
 	function loop(){
@@ -241,7 +252,7 @@ function getCacheB64(ids,src,callback) {
 }
 function getInjected(url,src,callback) {	// for injected
 	function getScripts(){
-		var o=db.transaction('scripts').objectStore('scripts'),n=0,require={};
+		var o=db.transaction('scripts').objectStore('scripts'),require={};
 		o.index('position').openCursor().onsuccess=function(e){
 			var i,r=e.target.result,v;
 			if(r) {
@@ -253,13 +264,7 @@ function getInjected(url,src,callback) {	// for injected
 					for(i in v.meta.resources) cache[v.meta.resources[i]]=1;
 				}
 				r.continue();
-			} else {
-				if(n&&src.url==src.tab.url) {
-					chrome.browserAction.setBadgeBackgroundColor({color:'#808',tabId:src.tab.id});
-					chrome.browserAction.setBadgeText({text:n.toString(),tabId:src.tab.id});
-				}
-				getRequire(Object.getOwnPropertyNames(require));
-			}
+			} else getRequire(Object.getOwnPropertyNames(require));
 		};
 	}
 	function getRequire(require){
@@ -292,8 +297,11 @@ function getInjected(url,src,callback) {	// for injected
 		var o=db.transaction('values').objectStore('values');
 		loop();
 	}
-	function finish(){callback(data);}
-	var data={scripts:[],values:{},require:{}},cache={},values=[];
+	function finish(){
+		callback(data);
+		if(n&&src.url==src.tab.url) chrome.tabs.sendMessage(src.tab.id,{cmd:'GetBadge'});
+	}
+	var data={scripts:[],values:{},require:{}},cache={},values=[],n=0;
 	if(data.isApplied=settings.isApplied) getScripts(); else finish();
 }
 function fetchURL(url, cb, type, headers) {
@@ -637,6 +645,7 @@ initDb(function(){
 			ParseScript: parseScript,
 			GetScript: getScript,	// for user edit
 			GetMetas: getMetas,	// for popup menu
+			SetBadge: setBadge,
 			AutoUpdate: autoUpdate,
 			Vacuum: vacuum,
 			Move: move,

+ 12 - 9
injected.js

@@ -26,22 +26,23 @@ function utf8decode (utftext) {
 }
 
 // Messages
-var id=Math.random();
-function post(data,callback){
-	if(!data.data) data.data={};
-	data.data['frameid']=id;
-	chrome.runtime.sendMessage(data);
-}
 chrome.runtime.onMessage.addListener(function(req,src) {
-	if(req.frameid&&req.frameid!=id) return;
 	var maps={
 		Command:command,
 		GetPopup:getPopup,
+		GetBadge:getBadge,
 	},f=maps[req.cmd];
 	if(f) f(req.data,src);
 });
 function getPopup(){
-	chrome.runtime.sendMessage({cmd:'SetPopup',data:[menu,ids]});
+	// XXX: only scripts run in top level window are counted
+	if(top===window)
+		chrome.runtime.sendMessage({cmd:'SetPopup',data:[menu,ids]});
+}
+function getBadge(){
+	// XXX: only scripts run in top level window are counted
+	if(top===window)
+		chrome.runtime.sendMessage({cmd:'SetBadge',data:ids.length});
 }
 
 // Communicator
@@ -409,7 +410,9 @@ var comm={
 },menu=[],ids=[];
 function handleC(e){
 	var o=JSON.parse(e.attrName),maps={
-		SetValue:function(o){post({cmd:'SetValue',data:o});},
+		SetValue:function(o){
+			chrome.runtime.sendMessage({cmd:'SetValue',data:o});
+		},
 		RegisterMenu:function(o){
 			if(window.top===window) menu.push(o);
 		},