os0x avatar os0x committed 624f6b8

Popupからもハイライトできるように

Comments (0)

Files changed (2)

 };
 if(this.chrome){
 chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
-	if (request === 'init') {
-		var tabid = sender.tab.id;
-		if (Config.auto_highlight){
+	var tabid = sender.tab.id;
+	if (request.action === 'init') {
+		if (!request.keyword) {
+			if (!Config.always_icon){
+				return;
+			}
+			chrome.pageAction.setIcon({tabId:tabid, path:'word_g.png'});
+			chrome.pageAction.setPopup({tabId:tabid, popup:'popup.html'});
+			chrome.tabs.executeScript(tabid,{code:'word_highlight({option:'+JSON.stringify(Config)+',keyword:""})'});
+		} else if (Config.auto_highlight){
 			sendResponse({option:Config});
 			chrome.pageAction.setIcon({tabId:tabid, path:'word.png'});
-			chrome.pageAction.setTitle({tabId:tabid, title:'word highlight / off'});
 			chrome.pageAction.setPopup({tabId:tabid, popup:'popup.html'});
 		} else {
 			chrome.pageAction.setIcon({tabId:tabid, path:'word_g.png'});
 		}
 		chrome.pageAction.show(tabid);
 	}
+	if (request.action === 'setuped') {
+		chrome.pageAction.setIcon({tabId:tabid, path:'word.png'});
+	}
 });
 chrome.pageAction.onClicked.addListener(function(tab){
 	chrome.pageAction.setIcon({tabId:tab.id, path:'word.png'});

words_highlight.js

 		return str.replace(/(?:(?:\s?(?:site|(?:all)?in(?:url|title|anchor|text)):|(?:\s|^)-|[()])\S*|(\s)OR\s)/g,'$1');
 	}
 }
-if (keyword) {
-	if(this.chrome){
-		chrome.extension.sendRequest(wordhighlightID,'init', word_highlight);
-	} else if(this.safari){
-		safari.self.tab.dispatchMessage('init',keyword);
-		safari.self.addEventListener('message',function(evt){
-			word_highlight(evt.message);
-		},false);
-	}
+var state = {
+	highlighted:true,
+	words:[]
+};
+if(this.chrome){
+	chrome.extension.sendRequest(wordhighlightID,{action:'init',keyword:keyword}, word_highlight);
+} else if(this.safari){
+	safari.self.tab.dispatchMessage('init',{action:'init',keyword:keyword});
+	safari.self.addEventListener('message',function(evt){
+		word_highlight(evt.message);
+	},false);
+}
+if (!keyword){
+	word_highlight.push_state_for_popup = function(){
+		chrome.extension.sendRequest({state: state});
+	};
 }
 function word_highlight(message){
-	if (message.keyword){
-		keyword = option.keyword;
+	if ('keyword' in message){
+		keyword = message.keyword;
 	}
-	window.name = PRE + '::' + encodeURIComponent(keyword);
 	if (document.contentType && !/html/i.test(document.contentType))
 		return;
-	var state = {
-		highlighted:true,
-		words:[]
-	};
 	var option = message.option;
-	var PRE = 'wordhighlight', ID_PRE = PRE + '_id';
+	var ID_PRE = PRE + '_id';
 	var STYLE_CLASS = '0123456789'.split('').map(function(a,i){return PRE + '_word'+i;});
 	var STYLE_COLOR = ['#FFFF80','#99ccff','#ff99cc','#66cc66','#cc99ff','#ffcc66','#669999','#cc9966','#999999','#cc6699'];
 	var setuped = false;
 		});
 		chrome.extension.sendRequest({state: state});
 	};
-	setup();
+	if (keyword) {
+		setup();
+	}
 	init();
 	if (option.popup) {
 		word_highlight.push_state_for_popup();
 		exprs = words.map(function(w, _i){
 			return new $XE('descendant::font[@name="' + PRE + '_word' + _i +'"]', document.body);
 		});
+		chrome.extension.sendRequest({action: 'setuped'});
+		if (keyword) {
+			window.name = PRE + '::' + encodeURIComponent(keyword);
+		}
 	}
 
 	function init(){
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.