横断検索サイト『検索デスク』のメニューに開閉機能を追加する。実用性があるかどうかは不明である。今回ちょっと困ったのは、検索デスクのHTMLが以下のようになっていた点である。
『検索デスク』のHTMLの構造
<h2>[サブタイトル1]</h2>
<div class="part-r">
<a>[検索サイトA]</a><input type="button"><br />
<a>[検索サイトB]</a><input type="button"><br />
</div>
<h2>[サブタイトル2]</h2>
<div class="part-r">
<a>[検索サイトC]</a><input type="button"><br />
<a>[検索サイトD]</a><input type="button"><br />
</div>
<div class="part-r">
<a>[検索サイトA]</a><input type="button"><br />
<a>[検索サイトB]</a><input type="button"><br />
</div>
<h2>[サブタイトル2]</h2>
<div class="part-r">
<a>[検索サイトC]</a><input type="button"><br />
<a>[検索サイトD]</a><input type="button"><br />
</div>
このため、h2要素から、div要素(class属性にpart-rを持つdiv)を取得するのに「nextSibling」を使わざるをえなかった。今回のような用途では、nextSiblingは本来使うべきではないと思う。次のようになっていれば、
<div>
<h2>[サブタイトル1]</h2>
<div class="part-r">
<a>[検索サイトA]</a><input type="button"><br />
<a>[検索サイトB]</a><input type="button"><br />
</div>
</div>
<div>
<h2>[サブタイトル2]</h2>
<div class="part-r">
<a>[検索サイトC]</a><input type="button"><br />
<a>[検索サイトD]</a><input type="button"><br />
</div>
</div>
<h2>[サブタイトル1]</h2>
<div class="part-r">
<a>[検索サイトA]</a><input type="button"><br />
<a>[検索サイトB]</a><input type="button"><br />
</div>
</div>
<div>
<h2>[サブタイトル2]</h2>
<div class="part-r">
<a>[検索サイトC]</a><input type="button"><br />
<a>[検索サイトD]</a><input type="button"><br />
</div>
</div>
データの意味上でも、レイアウト上でも問題なかったのではと思う。
/*
================================================================================
Name : 検索デスクの一覧を開閉式にするBookmarklet Ver1.0.0
In : [none]
Out : [none]
Note : 検索デスクの検索サイト一覧をカテゴリ毎に開閉可能にします。
--------------------------------------------------------------------------------
Version : Ver1.0.0 | 2006/12/29 | 新規作成
--------------------------------------------------------------------------------
License : New BSD license
URL : www.kanasansoft.com
================================================================================
*/
(
function(){
if((/^http:\/\/www.searchdesk.com\//).test(location.href)){
var
o=document.getElementsByTagName("h2")
,p
,f=function(a){
var n=a;
return function(){
n.style.display=n.style.display=="none"?"":"none";
}
}
;
for(var i=0;i<o.length;i++){
p=o[i].nextSibling;
while(p.nodeType!=1){
p=p.nextSibling
}
o[i].onclick=f(p);
o[i].style.padding="4px";
p.style.display="none";
}
}else{
alert("don\'t match location\n[SearchDesk]")
}
}
)
()
================================================================================
Name : 検索デスクの一覧を開閉式にするBookmarklet Ver1.0.0
In : [none]
Out : [none]
Note : 検索デスクの検索サイト一覧をカテゴリ毎に開閉可能にします。
--------------------------------------------------------------------------------
Version : Ver1.0.0 | 2006/12/29 | 新規作成
--------------------------------------------------------------------------------
License : New BSD license
URL : www.kanasansoft.com
================================================================================
*/
(
function(){
if((/^http:\/\/www.searchdesk.com\//).test(location.href)){
var
o=document.getElementsByTagName("h2")
,p
,f=function(a){
var n=a;
return function(){
n.style.display=n.style.display=="none"?"":"none";
}
}
;
for(var i=0;i<o.length;i++){
p=o[i].nextSibling;
while(p.nodeType!=1){
p=p.nextSibling
}
o[i].onclick=f(p);
o[i].style.padding="4px";
p.style.display="none";
}
}else{
alert("don\'t match location\n[SearchDesk]")
}
}
)
()