2008年7月7日月曜日

蝶最高

このエントリーをはてなブックマークに追加
昨日、Operaが最強になった日というエントリを書いたけども、一つだけ不満があった。
というのも、クイックリンクは数字キーを押すとデフォルトでその動作をしてしまう。
(1、2でタブの切替など)
どうせ、fしか使わないので、ちょっと書き直してみた。
あと、新しくウインドウ開くのがウザイので、同じウインドウ内で開くようにした。

f用

Go to page,"javascript:var bgcolor = '#FF0';var color = '#000';var hintlist = new Array();var hintedlinks = new Array();var map = new Array();var mapindex = 0;var choice = '';var keycodemapping = {'48':'0','49':'1','50':'2','51':'3','52':'4','53':'5','54':'6','55':'7','56':'8','57':'9','13':'Enter','27':'Esc','8':'Bkspc'};var originalTitle = document.title;function drawHints(){document.addEventListener('keypress',interpretKeyStroke,true);document.title+=' - ';var allLinks = document.getElementsByTagName('a');var viewportStart = window.pageYOffset - 5;var viewportEnd = viewportStart + window.innerHeight + 10;for (var i = 0;i<allLinks.length; i++){linkYcoord = getAbsoluteY(allLinks[i]);if(linkYcoord > viewportStart && linkYcoord < viewportEnd && allLinks[i].href != '') {hintedlinks.push(allLinks[i]);}}for (var i = 0;i<hintedlinks.length; i++){var hint = document.createElement('span');hintlist.push(hint);hint.style.backgroundColor=bgcolor;hint.style.color=color;hint.style.position='absolute';hint.innerHTML = mapindex;map[mapindex]=hintedlinks[i].href;mapindex++;hintedlinks[i].appendChild(hint,hintedlinks[i]);}}function getAbsoluteY(element){var y = 0;while (element) {y += element.offsetTop;element = element.offsetParent;}return y;}function removeHints(){for (var i=0; i<hintedlinks.length; i++){hintedlinks[i].removeChild(hintlist[i],hintedlinks[i]);}choice='';document.title=originalTitle;document.removeEventListener('keypress',interpretKeyStroke,true);delete map;delete hintlist;delete hintedlinks;}function getURLchoice(){return (map[choice]==undefined) ? False : map[choice];}function interpretKeyStroke(e){e.preventDefault();var key=keycodemapping[(typeof event!='undefined')?window.event.keyCode:e.keyCode];if(key=='Enter'){location.href=getURLchoice();removeHints();}else if(key=='Esc'){removeHints();}else if(key=='Bkspc'){choice=choice.slice(0,-1);document.title=document.title.slice(0,-1);}else if(key == undefined){removeHints();}else{choice+=key;document.title+=key;if((''+choice).length>=(''+mapindex).length){location.href=getURLchoice();removeHints();}}}drawHints();"

ポイントは、interpretKeyStrokeの次に書いてあるe.preventDefault。
これで、Operaのデフォルトキーを無効に出来る。

追記
;用も作った

Go to page,"javascript:var bgcolor = '#FF0';var bghighlight = '#0F0';var color = '#000';var hintlist = new Array();var hintedlinks = new Array();var map = new Array();var choices = new Array();var mapindex = 0;var choice = '';var keycodemapping = {'48':'0','49':'1','50':'2','51':'3','52':'4','53':'5','54':'6','55':'7','56':'8','57':'9','13':'Enter','27':'Esc','8':'Bkspc','44':','};var originalTitle = document.title;function drawHints(){document.addEventListener('keypress',interpretKeyStroke,true);document.title+=' - ';var allLinks = document.getElementsByTagName('a');var viewportStart = window.pageYOffset - 5;var viewportEnd = viewportStart + window.innerHeight + 10;for (var i = 0;i<allLinks.length; i++){linkYcoord = getAbsoluteY(allLinks[i]);if(linkYcoord > viewportStart && linkYcoord < viewportEnd && allLinks[i].href != '') {hintedlinks.push(allLinks[i]);}}for (var i = 0; i<allLinks.length; i++){var hint = document.createElement('span');hintlist.push(hint);hint.style.backgroundColor=bgcolor;hint.style.color=color;hint.style.position='absolute';hint.innerHTML = mapindex;map[mapindex]=hintedlinks[i].href;mapindex++;hintedlinks[i].appendChild(hint,hintedlinks[i]);}}function getAbsoluteY(element){var y = 0;while (element) {y += element.offsetTop;element = element.offsetParent;}return y;}function removeHints(){for (var i=0; i<hintedlinks.length;i++){hintedlinks[i].removeChild(hintlist[i],hintedlinks[i]);}choice='';document.title=originalTitle;document.removeEventListener('keypress',interpretKeyStroke,true);delete map;delete hintlist;delete hintedlinks;delete choices;}function interpretKeyStroke(e){e.preventDefault();var key=keycodemapping[(typeof event!='undefined')?window.event.keyCode:e.keyCode];if(key=='Enter'){choices.push(choice);for (var i=0;i<choices.length;i++) {if(map[choices[i]]!=undefined){window.open(map[choices[i]]);}}removeHints();}else if(key==','){choices.push(choice);hintlist[choice].style.backgroundColor=bghighlight;choice = '';document.title+=',';}else if(key=='Esc'){removeHints();}else if(key=='Bkspc'){choice = (document.title.charAt(document.title.length-1)==',') ? choices.pop() : choice.slice(0,-1);hintlist[choice].style.backgroundColor=bgcolor;document.title=document.title.slice(0,-1);}else if(key == undefined){removeHints();}else{choice+=key;document.title+=key;}}drawHints();"


結論。
蝶最高

16:16追記
ごめん、<と>間違ってたorz
修正しますた。

0 件のコメント :

コメントを投稿