Commits

os0x  committed 6df55cf

impl highlight score, fix bugs

  • Participants
  • Parent commits 624f6b8

Comments (0)

Files changed (18)

File background.html

 	always_icon:false,
 	auto_highlight:true
 };
-this.WordByTab = {};
 this.defaultConfig = JSON.parse(JSON.stringify(Config));
 get_manifest(function(manifest){
 	this.Manifest = manifest;
 } else {
 	localStorage.Config = JSON.stringify(Config);
 }
+if (!('auto_highlight' in Config)){
+	Config.auto_highlight = true;
+	localStorage.Config = JSON.stringify(Config);
+}
 this.Utils = {
 	update:function() {
 		localStorage.Config = JSON.stringify(Config);
 	var tabid = sender.tab.id;
 	if (request.action === 'init') {
 		if (!request.keyword) {
-			if (!Config.always_icon){
+			if (!Config.always_icon || request.isframe) {
 				return;
 			}
-			chrome.pageAction.setIcon({tabId:tabid, path:'word_g.png'});
+			chrome.pageAction.setIcon({tabId:tabid, path:'wg.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.setIcon({tabId:tabid, path:'w.png'});
 			chrome.pageAction.setPopup({tabId:tabid, popup:'popup.html'});
 		} else {
-			chrome.pageAction.setIcon({tabId:tabid, path:'word_g.png'});
+			chrome.pageAction.setIcon({tabId:tabid, path:'wg.png'});
 		}
 		chrome.pageAction.show(tabid);
 	}
 	if (request.action === 'setuped') {
-		chrome.pageAction.setIcon({tabId:tabid, path:'word.png'});
+		chrome.pageAction.setIcon({tabId:tabid, path:'w.png'});
 	}
 });
 chrome.pageAction.onClicked.addListener(function(tab){
-	chrome.pageAction.setIcon({tabId:tab.id, path:'word.png'});
+	chrome.pageAction.setIcon({tabId:tab.id, path:'w.png'});
 	chrome.pageAction.setTitle({tabId:tab.id, title:'word highlight / off'});
 	chrome.tabs.executeScript(tab.id,{code:'word_highlight({option:'+JSON.stringify(Config)+'})'});
 	chrome.pageAction.setPopup({tabId:tab.id, popup:'popup.html'});
 });
 }
-/*
-chrome.tabs.onUpdated.addListener(function(tabid,info,tab){
-	if (info.status === 'complete' && tab.url && tab.url.indexOf('http') === 0) {
-		var opt = WordByTab[tabid];
-		var keyword = opt && opt.keyword;
-		chrome.tabs.executeScript(tabid,{
-			code:init_keyword_source + '("' + (keyword||'') + '")',
-			allFrames:true
-		},function(){
-			if (WordByTab[tabid]) {
-				if (Config.sticky_off) {
-					chrome.pageAction.setIcon({tabId:tabid,path:'word_g.png'});
-					chrome.pageAction.setTitle({tabId:tabid,title:'word highlight / off'});
-					chrome.pageAction.show(tabid);
-				} else if (!WordByTab[tabid].off) {
-					excuteHighlight(tabid);
-				} else {
-					chrome.pageAction.show(tabid);
-				}
-			} else {
-				chrome.pageAction.hide(tabid);
-			}
-		});
-	}
-});
-function excuteHighlight(tabid, popup){
-	chrome.pageAction.show(tabid);
-	chrome.pageAction.setTitle({tabId:tabid,title:'word highlight / on'});
-	var opt = WordByTab[tabid];
-	if (popup) {
-		opt.popup = true;
-	}
-	var code = 'window.Options = ' + JSON.stringify(opt,null,'\t') + ';';
-	chrome.tabs.executeScript(tabid,{
-		code:code,
-		allFrames:true
-	},function(){
-		chrome.tabs.executeScript(tabid,{
-			code:source,
-			allFrames:true
-		});
-	});
-}
-chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
-	if (request.keyword && (!WordByTab[sender.tab.id] || !WordByTab[sender.tab.id].off) ) {
-		WordByTab[sender.tab.id] = {keyword:request.keyword};
-	}
-});
-*/
 function get_manifest(callback){
 	var url = './manifest.json';
 	var xhr = new XMLHttpRequest();

File manifest.json

 {
    "background_page": "background.html",
    "icons": {
-      "128": "word128.png",
-      "16": "word16.png",
-      "32": "word32.png",
-      "64": "word64.png"
+      "128": "w128.png",
+      "48": "w48.png",
+      "16": "w.png"
    },
   "content_scripts": [
     {
    "name": "word highlight",
    "options_page": "options_page.html",
    "page_action": {
-      "default_icon": "word.png",
+      "default_icon": "w.png",
       "default_title": "word highlight"
    },
    "permissions": [ "tabs" ,"http://*/", "https://*/"],
-   "version": "1.1.0",
+   "version": "1.2.0",
    "id": "ooabkmkhabkahcjbgpiajffckeibpdoa"
 }

File option_page.css

 button[disabled]{
 cursor:default;
 }
-section.content > p.sub{
+section.content > div{
+margin:1em 0.5em;
+}
+section.content > div.sub{
 margin-left:2em;
 }
 #AccelerationValue{

File options_page.html

-<!DOCTYPE html>
-<html>
-<head>
-<meta charset="utf-8">
-<title>word highlight</title>
-<script type="text/javascript" src="x.js"></script>
-<script type="text/javascript" src="tween2.js"></script>
-<script type="text/javascript" src="chrome-extension://okneonigbfnolfkmfgjmaeniipdjkgkl/chrome_keyconfig.js"></script>
-<script type="text/javascript" src="chrome-extension://jpkfjicglakibpenojifdiepckckakgk/chrome_gestures.user.js"></script>
-<link href="option_page.css" rel="stylesheet" type="text/css">
-<body>
-<ul class="tabs" id="menu_tabs">
-	<li class="basics"><a href="#basics" class="active"><span>Basics</span></a>
-	<li class="about"><a href="#about"><span>About</span></a>
-</ul>
-<div id="container">
-<div id="inner_container">
-<section id="basics" class="content">
-	<div><input type="checkbox" id="auto_highlight"><label for="auto_highlight">auto highlight</label></div>
-	<div><input type="checkbox" id="always_icon"><label for="always_icon">Show wordhighlight icon always</label></div>
-</section>
-<section id="about" class="content">
-	<dl>
-		<dt>Name:<dt>
-		<dd><a href="https://chrome.google.com/extensions/detail/ooabkmkhabkahcjbgpiajffckeibpdoa" target="_blank">word highlight</a></dd>
-		<dt>Version:<dt>
-		<dd>ver <span id="ExtensionVersion"></span></dd>
-		<dt>Author:<dt>
-		<dd><a href="http://ss-o.net/" target="_blank">os0x</a></dd>
-		<dt>License:<dt>
-		<dd><a href="http://creativecommons.org/licenses/MIT/" target="_blank">The MIT license</a></dd>
-	</dl>
-</section>
-</div>
-</div>
-<script>
-window.onload = function(){
-BackGround = chrome.extension.getBackgroundPage();
-Config = BackGround.Config;
-Utils = BackGround.Utils;
-function L10N(){
-	chrome.i18n.getAcceptLanguages(function(langs){
-		if (langs.indexOf('ja') < 0 ) {
-			document.querySelector('#menu_tabs > li.news').style.display = 'none';
-		}
-	});
-	var labels = document.querySelectorAll('label')
-	for (var i = 0; i < labels.length; i++){
-		var message = chrome.i18n.getMessage('options_' + labels[i].htmlFor);
-		if (message) {
-			labels[i].innerHTML = message;
-		}
-	}
-}
-//L10N();
-
-var WIDTH = 800;
-var HEIGHT = Math.max(window.innerHeight - 100, 500);
-
-$X('/html/body/div/div/section/div/input[@type="checkbox"]').forEach(function(box){
-	var id = box.id;
-	var val = Config[id];
-	if (val === true || val === false) {
-		box.checked = val;
-	} else {
-		//return;
-	}
-	box.addEventListener('click',function(){
-		if (box.checked) {
-			Config[id] = true;
-		} else {
-			Config[id] = false;
-		}
-		Utils.update();
-	},false);
-});
-
-$X('/html/body/div/div/section/div/input[@type="text"]').forEach(function(input){
-	var id = input.id;
-	input.value = Config[id];
-	input.addEventListener('change',function(){
-		Config[id] = input.value;
-		Utils.update();
-	},false);
-});
-
-document.getElementById('ExtensionVersion').textContent = BackGround.Manifest.version;
-
-
-var sections = $X('/html/body/div/div/section[contains(@class, "content")]');
-var inner_container = document.getElementById('inner_container');
-var container = document.getElementById('container');
-inner_container.style.width = sections.length * (WIDTH+20) + 'px';
-
-container.style.marginTop = '-2px';
-sections.forEach(function(section, _i){
-	section.style.visibility = 'hidden';
-	section.style.height = '100px';
-});
-var btns = $X('id("menu_tabs")/li/a');
-var default_title = document.title;
-btns.forEach(function(btn, i, btns){
-	btn.addEventListener('click',function(evt){
-		evt.preventDefault();
-		btns.forEach(function(btn){btn.className = '';})
-		btn.className = 'active';
-		sections[i].style.visibility = 'visible';
-		sections[i].style.height = 'auto';
-		new Tween(inner_container.style, {marginLeft:{to:i * -WIDTH,tmpl:'$#px'},time:0.2,onComplete:function(){
-				document.title = default_title + btn.hash;
-				location.hash = btn.hash;
-				window.scrollBy(0, -1000);
-				sections.forEach(function(section, _i){
-					if (i !== _i) {
-						section.style.visibility = 'hidden';
-						section.style.height = '100px';
-					}
-				});
-			}});
-	}, false);
-});
-if (location.hash) {
-	sections.some(function(section, i){
-		if ('#' + section.id === location.hash) {
-			btns.forEach(function(btn){btn.className = '';})
-			btns[i].className = 'active';
-			inner_container.style.marginLeft = -WIDTH * i + 'px';
-			section.style.visibility = 'visible';
-			section.style.height = 'auto';
-			document.title = default_title + location.hash;
-		}
-	});
-} else {
-	sections[0].style.height = 'auto';
-	sections[0].style.visibility = 'visible';
-	document.title = default_title + '#' + sections[0].id;
-}
-};
-</script>
-</body>
-</html>
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>word highlight</title>
+<script type="text/javascript" src="x.js"></script>
+<script type="text/javascript" src="tween2.js"></script>
+<script type="text/javascript" src="chrome-extension://okneonigbfnolfkmfgjmaeniipdjkgkl/chrome_keyconfig.js"></script>
+<script type="text/javascript" src="chrome-extension://jpkfjicglakibpenojifdiepckckakgk/chrome_gestures.user.js"></script>
+<link href="option_page.css" rel="stylesheet" type="text/css">
+<body>
+<ul class="tabs" id="menu_tabs">
+	<li class="basics"><a href="#basics" class="active"><span>Basics</span></a>
+	<li class="news"><a href="#news"><span>News</span></a>
+	<li class="about"><a href="#about"><span>About</span></a>
+</ul>
+<div id="container">
+<div id="inner_container">
+<section id="basics" class="content">
+	<div><input type="checkbox" id="auto_highlight"><label for="auto_highlight">auto highlight</label></div>
+	<div><input type="checkbox" id="always_icon"><label for="always_icon">show wordhighlight icon always</label></div>
+</section>
+<section id="news" class="content">
+<ul>
+	<li>
+		<h4>2011/03/27</h4>
+		<p>ヒット件数の多いキーワードはハイライトを弱くするようにしました。
+		<p>ハイライト処理のバグ修正など
+	<li>
+		<h4>2010/06/13</h4>
+		<p>Google Mapsなどで重くなる問題を修正しました。
+</ul>
+</section>
+<section id="about" class="content">
+	<dl>
+		<dt>Name:<dt>
+		<dd><a href="https://chrome.google.com/extensions/detail/ooabkmkhabkahcjbgpiajffckeibpdoa" target="_blank">word highlight</a></dd>
+		<dt>Version:<dt>
+		<dd>ver <span id="ExtensionVersion"></span></dd>
+		<dt>Author:<dt>
+		<dd><a href="http://ss-o.net/" target="_blank">os0x</a></dd>
+		<dt>License:<dt>
+		<dd><a href="http://creativecommons.org/licenses/MIT/" target="_blank">The MIT license</a></dd>
+	</dl>
+</section>
+</div>
+</div>
+<script>
+window.onload = function(){
+BackGround = chrome.extension.getBackgroundPage();
+Config = BackGround.Config;
+Utils = BackGround.Utils;
+function L10N(){
+	chrome.i18n.getAcceptLanguages(function(langs){
+		if (langs.indexOf('ja') < 0 ) {
+			document.querySelector('#menu_tabs > li.news').style.display = 'none';
+		}
+	});
+	var elems = document.querySelectorAll('*[class^="MSG_"]');
+	Array.prototype.forEach.call(elems, function(node){
+		var key = node.className.match(/MSG_(\w+)/)[1];
+		var message = chrome.i18n.getMessage(key);
+		if (message) node.textContent = message;
+	});
+}
+L10N();
+
+var WIDTH = 800;
+var HEIGHT = Math.max(window.innerHeight - 100, 500);
+
+$X('/html/body/div/div/section/div/input[@type="checkbox"]').forEach(function(box){
+	var id = box.id;
+	var val = Config[id];
+	if (val === true || val === false) {
+		box.checked = val;
+	} else {
+		//return;
+	}
+	box.addEventListener('click',function(){
+		if (box.checked) {
+			Config[id] = true;
+		} else {
+			Config[id] = false;
+		}
+		Utils.update();
+	},false);
+});
+
+$X('/html/body/div/div/section/div/input[@type="text"]').forEach(function(input){
+	var id = input.id;
+	input.value = Config[id];
+	input.addEventListener('change',function(){
+		Config[id] = input.value;
+		Utils.update();
+	},false);
+});
+
+document.getElementById('ExtensionVersion').textContent = BackGround.Manifest.version;
+
+
+var sections = $X('/html/body/div/div/section[contains(@class, "content")]');
+var inner_container = document.getElementById('inner_container');
+var container = document.getElementById('container');
+inner_container.style.width = sections.length * (WIDTH+20) + 'px';
+
+container.style.marginTop = '-2px';
+sections.forEach(function(section, _i){
+	section.style.visibility = 'hidden';
+	section.style.height = '100px';
+});
+var btns = $X('id("menu_tabs")/li/a');
+var default_title = document.title;
+btns.forEach(function(btn, i, btns){
+	btn.addEventListener('click',function(evt){
+		evt.preventDefault();
+		btns.forEach(function(btn){btn.className = '';})
+		btn.className = 'active';
+		sections[i].style.visibility = 'visible';
+		sections[i].style.height = 'auto';
+		new Tween(inner_container.style, {marginLeft:{to:i * -WIDTH,tmpl:'$#px'},time:0.2,onComplete:function(){
+				document.title = default_title + btn.hash;
+				location.hash = btn.hash;
+				window.scrollBy(0, -1000);
+				sections.forEach(function(section, _i){
+					if (i !== _i) {
+						section.style.visibility = 'hidden';
+						section.style.height = '100px';
+					}
+				});
+			}});
+	}, false);
+});
+if (location.hash) {
+	sections.some(function(section, i){
+		if ('#' + section.id === location.hash) {
+			btns.forEach(function(btn){btn.className = '';})
+			btns[i].className = 'active';
+			inner_container.style.marginLeft = -WIDTH * i + 'px';
+			section.style.visibility = 'visible';
+			section.style.height = 'auto';
+			document.title = default_title + location.hash;
+		}
+	});
+} else {
+	sections[0].style.height = 'auto';
+	sections[0].style.visibility = 'visible';
+	document.title = default_title + '#' + sections[0].id;
+}
+};
+</script>
+</body>
+</html>

File w.png

Added
New image

File w128.png

Added
New image

File w32.png

Added
New image

File w48.png

Added
New image

File w64.png

Added
New image

File wg.png

Added
New image

File word.png

Removed
Old image

File word128.png

Removed
Old image

File word16.png

Removed
Old image

File word32.png

Removed
Old image

File word64.png

Removed
Old image

File word_g.png

Removed
Old image

File words_highlight.js

 // console.time("highlight");
 var wordhighlightID = 'ooabkmkhabkahcjbgpiajffckeibpdoa';
-var keyword, PRE = 'wordhighlight';
+var keyword = '', PRE = 'wordhighlight';
+var id_index = 0;
 init_keyword();
 function init_keyword(){
 	var host = location.host, ref = document.referrer;
 	words:[]
 };
 if(this.chrome){
-	chrome.extension.sendRequest(wordhighlightID,{action:'init',keyword:keyword}, word_highlight);
+	chrome.extension.sendRequest(wordhighlightID,{action:'init',keyword:keyword, isframe:window!==window.top}, word_highlight);
 } else if(this.safari){
 	safari.self.tab.dispatchMessage('init',{action:'init',keyword:keyword});
 	safari.self.addEventListener('message',function(evt){
 		return;
 	var option = message.option;
 	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 STYLE_CLASS = '0123456789'.split('').map(function(a,i){return PRE + '_ext ' + PRE + '_word'+i;});
+	//var STYLE_COLOR = ['#FFFF80',    '#99ccff',    '#ff99cc'    ,'#66cc66'    ,'#cc99ff'    ,'#ffcc66'    ,'#669999'    ,'#cc9966'    ,'#999999'    ,'#cc6699'];
+	var STYLE_COLOR = ['255,255,128','153,204,255','255,153,204','102,204,102','204,153,255','255,204,102','102,153,153','204,153,102','153,153,153','204,102,153'];
 	var setuped = false;
 	var words = [], layers, positions = [], exprs = [];
 	var xp_all = new $XE('descendant::font[starts-with(@name,"' + PRE + '_word")]', document.body);
 			});
 			xw = ' and (' + _words.map(function(w){return ' contains(translate(self::text(),"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ"),'+escapeXPathExpr(w.text.toUpperCase())+') ';}).join(' or ') + ') ';
 		}
-		$X('descendant::text()[string-length(normalize-space(self::text())) > 0 ' + xw +' and not(ancestor::textarea or ancestor::script or ancestor::style or ancestor::aside)]', doc).forEach(function(text_node) {
-			var df, text = text_node.nodeValue, id_index = 0,
-			parent = text_node.parentNode, range = document.createRange(), replace_strings = [],
-			new_text = exd_words.reduce(function(text,ew,i) {
-				if (!ew) return text;
-				var _i = ew.index || i;
-				return text.replace(ew.regexp,function($0,$1) {
-					replace_strings[id_index] = '<font id="' + ID_PRE + id_index + '" class="' + STYLE_CLASS[_i%10] + '" name="'+PRE+'_word'+_i+'">' + $1 + '</font>';
-					return '##'+(id_index++)+'##';
-				});
-			}, text).
-				replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').
-				replace(/##(\d+)##/g, function($0,$1) {
-				return replace_strings[$1] || '';
+		var not_element = ['ancestor::textarea', 'ancestor::script' ,'ancestor::style' ,'ancestor::aside' ,'ancestor::font[contains(@class,"'+PRE+'")]'];
+		var bpre = $X('//pre[contains(@class,"brush:")]');
+		if (bpre.length){
+			var not_element_cp = not_element.slice();
+			not_element.push('ancestor::pre[contains(@class,"brush:")]');
+			window.addEventListener('load',function(){
+				setTimeout(function(){
+					$X('descendant::text()[string-length(normalize-space(self::text())) > 3 ' + xw +' and not(' + not_element.join(' or ') + ')]', doc).forEach(_highlight);
+				},100);
+			},false);
+		}
+		$X('descendant::text()[string-length(normalize-space(self::text())) > 3 ' + xw +' and not(' + not_element.join(' or ') + ')]', doc).forEach(_highlight);
+		function _highlight(node) {
+			exd_words.forEach(function(ew, _i){
+				if (node.nodeValue.search(ew.regexp) >= 0) {
+					var text = node.nodeValue, index;
+					var parent = node.parentNode;
+					while (text && (index = text.search(ew.regexp)) >= 0) {
+						var _end = RegExp.$1.length;
+						var _txt = node.splitText(index);
+						var __txt = _txt.splitText(_end);
+						var ft = document.createElement('font');
+						ft.id =  ID_PRE + id_index++;
+						ft.className = STYLE_CLASS[_i%10];
+						ft.setAttribute('name', PRE + '_word' + _i);
+						ft.appendChild(_txt);
+						parent.insertBefore(ft, __txt);
+						text = __txt.nodeValue;
+						node = __txt;
+					}
+				}
 			});
-			if (replace_strings.length) {
-				range.selectNodeContents(document.documentElement);
-				df = range.createContextualFragment(new_text);
-				if (df.firstChild) parent.replaceChild(df, text_node);
-				range.detach();
-			}
-		});
+		}
 	}
 
 	function setup(){
 	}
 
 	function init(){
+		var PAGERATE = Math.ceil(root.scrollHeight / 10000) + 2;
 		var sheet = addCSS(STYLE_COLOR.map(function(rgb,i){
-			return 'font.' + PRE + '_word'+i+'{display:inline;background:'+rgb+';color:black;}';
+			var hits = exprs[i] && exprs[i].get().length;
+			var alpha = hits >= 1 ? (Math.max((1 - Math.log(hits)*Math.LOG10E / PAGERATE) , 0.25)).toFixed(2) : 1;
+			return 'font.' + PRE + '_word'+i+'{display:inline;background:rgba('+rgb+','+alpha+');color:black;}';
 		}).join('\n'),'word_highlight_style');
 		init_autopager();
 		document.addEventListener('word_highlight.reset',function(e){