右クリックで「モヒカン族」

観測気球

収集物の記録書庫 a data archive of collection -- collectible toys

[要旨] モヒカン族に関する言及を見つけたら、右クリックで即クリップ!! ハンドアックス投擲支援スクリプトです。
[キーワード] モヒカン族,Javascript,はてなグループ

« Yahoo! ショッピング、MyLink開始 | トップページ | 抹茶スフレロール/スヴニールママン »

2005.07.07

右クリックで「モヒカン族」

こんなの、モヒカン族グループの人にしか使い道がないけど、他の人にもそれなりに参考になるかもしれないので、恥ずかしいソースですが公開します。

何のスクリプトかというと、掲示板『モヒカン族 - 「モヒカン族」に関する言及』に投稿するスクリプトです。

グループの URL が含まれる部分辺りをいじれば、他のはてなグループにも流用可能です。掲示板のスレッドIDは、ハードコーディングで 2 固定になっていますが、お好きなように変更してください。

それではソース、行きます。モヒカン族対象なので、詳しくはソースを見てね、ってことで、これ以上説明は書きません。リファクタリングして、ハンドアックスを送り返していただけると嬉しいです。

<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" language="JavaScript">

// 「右クリックで モヒカン族」
//     written by H.Tsujimura (tsupo@na.rim.or.jp)
//
//    モヒカン族掲示板に報告を投稿するためのスクリプトです。
//    殺伐と言及チェックしようぜ!!
//
//     Version 1.0  7 Jul 2005  最初の版
//  注意: このスクリプトを利用するには、「はてなグループ - モヒカン族」に
//        参加する必要があります。
//          はてなグループ - モヒカン族 http://mohican.g.hatena.ne.jp/

obj = external.menuArguments;   // 右クリック時の引数のオブジェクトの参照
page_url  = obj.document.URL;   // ページのURL
title_txt = obj.document.title; // タイトル文字列
cite_txt  = obj.document.selection.createRange().text; // 引用文字列
cite_html = obj.document.selection.createRange().htmlText;
inn       = obj.document.body.innerHTML;
inn_html  = obj.document.body.createTextRange().htmlText;

if ( obj.location.href && (obj.location.href != "") )
    page_url = obj.location.href; // 「はてなダイアリーキーワード」対策

txt = "";
function getTarget()
{
    rest = txt;
    s = txt.indexOf("<");
    while ( (s >= 0) && (s <= 2) ) {
        e = txt.indexOf(">");
        txt = txt.substring(e+1,txt.length);
        s = txt.indexOf("<");
    }
    if ( s > 0 ) {
        target = txt.substring(0,s);
        rest   = txt.substring(s,txt.length);
    }
    else {
        target = txt;
        rest   = "";
    }
    txt = rest;

    return ( target );
}

function adjustTarget(target)
{
    /* for Bloglines */
    q = target.indexOf('"'); // "
    if ( q > 0 )
        target = target.substring(0,q);
    else if ( q == 0 )
        target = target.substring(1,target.length);

    q = target.indexOf(' ');
    if ( q > 0 )
        target = target.substring(0,q);
    else if ( q == 0 )
        target = target.substring(1,target.length);

    q = target.indexOf('\\x0A');
    if ( q > 0 )
        target = target.substring(0,q);
    else if ( q == 0 )
        target = target.substring(4,target.length);

    q = target.indexOf('\\x0D');
    if ( q > 0 )
        target = target.substring(0,q);
    else if ( q == 0 )
        target = target.substring(4,target.length);

    do {
        q = target.indexOf('/');
        if ( q >= 0 )
            target = target.substring(0,q) + '\\x2F' + target.substring(q+1,target.length);
        else
            break;
    } while ( true );

    return ( target );
}

if ( (cite_txt != "") && (cite_txt.length > 0) ) {
    if ( page_url.match("http://www.bloglines.com/myblogs_display") ||
         page_url.match("http://bloglines.com/myblogs_display")        ) {
        /* Bloglines で読んでいるテキスト(記事、クリップ、など)を取り込み可能にするための処理 */
        target1 = "";
        target2 = "";
        target3 = "";
        target4 = "";
        target5 = "";
        if ( cite_txt == cite_html )
            target1 = cite_txt;
        else {
            /* target1 */
            txt = cite_html;
            target1 = getTarget();
            cite_html = txt;

            if ( target1 != "" ) {
                target1 = adjustTarget(target1);

                txt = cite_html;
                target2 = getTarget();
                cite_html = txt;
            }

            if ( target2 != "" ) {
                target2 = adjustTarget(target2);

                txt = cite_html;
                target3 = getTarget();
                cite_html = txt;
            }

            if ( target3 != "" ) {
                target3 = adjustTarget(target3);

                txt = cite_html;
                target4 = getTarget();
                cite_html = txt;
            }

            if ( target4 != "" ) {
                target4 = adjustTarget(target4);

                txt = cite_html;
                target5 = getTarget();
                cite_html = txt;
            }

            if ( target5 != "" ) {
                target5 = adjustTarget(target5);
            }
        }

        loc  = 0;
        aim  = inn;
        done = false;
        s_summary = 0;
        do {
            s_summary = aim.indexOf("itemText+=\"<p><table cellspacing=0 cellpadding=0 width=\\\"100%\\\">");
            if ( s_summary >= 0 ) {
                e_summary = aim.indexOf("itemText+=\"<div class=\\\"item_links\\\">\";",s_summary);
                if ( s_summary < e_summary ) {
                    aim2 = aim.substring(s_summary+64,e_summary);
                    if ( aim2.indexOf("\\x3C") == -1 ) {
                        // 主として「人力検索サイトはてな」対策
                        //   -- ちなみに、aim2.indexOf("\\x3C") >= 0 になるのは、MM/Memo とか はてなブックマーク --
                        //   -- とか 記事本文(冒頭)が a 要素、blockquote要素等でマークアップされている場合       --
                        txt  = aim2;
                        aim2 = getTarget();
                        aim2 = adjustTarget(aim2);
                        // 注意: 上記処理の結果、aim2.length < target?.length となる可能性が発生
                    }
                    if ( (aim2.indexOf(target1) == -1) && (target1.indexOf(aim2) == -1) ) {
                        aim = aim.substring(e_summary+39,aim.length);
                        loc += (e_summary+39);
                        continue;
                    }
                    if ( (target2 != "") && (aim2.indexOf(target2) == -1) && (target2.indexOf(aim2) == -1) ) {
                        aim = aim.substring(e_summary+39,aim.length);
                        loc += (e_summary+39);
                        continue;
                    }
                    if ( (target3 != "") && (aim2.indexOf(target3) == -1) && (target3.indexOf(aim2) == -1) ) {
                        aim = aim.substring(e_summary+39,aim.length);
                        loc += (e_summary+39);
                        continue;
                    }
                    if ( (target4 != "") && (aim2.indexOf(target4) == -1) && (target4.indexOf(aim2) == -1) ) {
                        aim = aim.substring(e_summary+39,aim.length);
                        loc += (e_summary+39);
                        continue;
                    }
                    if ( (target5 != "") && (aim2.indexOf(target5) == -1) && (target5.indexOf(aim2) == -1) ) {
                        aim = aim.substring(e_summary+39,aim.length);
                        loc += (e_summary+39);
                        continue;
                    }
                    done = true;
                }
                else
                    break;
            }
            else
                break;
        } while ( done == false );

        if ( s_summary > 0 ) {
            if ( loc >= 0 ) {
                locH3 = inn.substring(0,loc+s_summary).lastIndexOf("a title=\\\"サイト"); // "
                if ( locH3 == -1 )
                    locH3 = inn.substring(0,loc+s_summary).lastIndexOf("a title=\\\"Site"); // "
                if ( locH3 > 0 ) {
                    targetString = inn.substring(locH3,loc+s_summary);

                    siteName   = "";
                    s_siteName = targetString.indexOf(": ");
                    e_siteName = targetString.indexOf("\\\" href=");
                    if ( (s_siteName > 0) && (s_siteName < e_siteName) )
                        siteName = targetString.substring(s_siteName+2,e_siteName);

                    if ( targetString.match( "</a>") == null )
                        targetString = inn.substring(locH3,locH3+inn.substring(locH3,inn.length).indexOf("</a>")+4);
                    locHREF = targetString.indexOf("href=");
                    if ( locHREF > 0 ) {
                        targetHREF = targetString.substring(locHREF + 7,targetString.indexOf('\\" target='));
                        s = targetString.indexOf('" target=_blank>');
                        e = targetString.indexOf("</a>");
                        if ( (s > 0) && (s < e) )
                            targetTITLE = targetString.substring(targetString.indexOf('" target=_blank>')+16,targetString.indexOf("</a>"));
                        else
                            targetTITLE = "";
                        if ( targetHREF != "" )
                            page_url  = targetHREF;
                        if ( targetTITLE != "" ) {
                            if ( siteName != "" )
                                title_txt = siteName + ': ' + targetTITLE;
                            else
                                title_txt = targetTITLE;
                        }
                    }
                }
            }
        }
    }
    else if ( page_url.match( "http://r.hatena.ne.jp/" ) ) {
        /* はてなRSS で読んでいるテキスト(記事、クリップ、など)を取り込み可能にするための処理 */
        target1 = "";
        target2 = "";
        target3 = "";
        target4 = "";
        target5 = "";
        if ( cite_txt == cite_html )
            target1 = cite_txt;
        else {
            /* target1 */
            txt = cite_html;
            target1 = getTarget();
            cite_html = txt;

            if ( target1 != "" ) {
                txt = cite_html;
                target2 = getTarget();
                cite_html = txt;
            }

            if ( target2 != "" ) {
                txt = cite_html;
                target3 = getTarget();
                cite_html = txt;
            }

            if ( target3 != "" ) {
                txt = cite_html;
                target4 = getTarget();
                cite_html = txt;
            }

            if ( target4 != "" ) {
                txt = cite_html;
                target5 = getTarget();
                cite_html = txt;
            }
        }

        loc  = 0;
        aim  = inn;
        done = false;
        s_summary = 0;
        do {
            s_summary = aim.indexOf("<DIV class=summary>");
            if ( s_summary >= 0 ) {
                e_summary = aim.indexOf("</DIV>",s_summary);
                if ( s_summary < e_summary ) {
                    aim2 = aim.substring(s_summary+19,e_summary);
                    if ( aim2.indexOf(target1) == -1 ) {
                        aim = aim.substring(e_summary+6,aim.length);
                        loc += (e_summary+6);
                        continue;
                    }
                    if ( (target2 != "") && (aim2.indexOf(target2) == -1) ) {
                        aim = aim.substring(e_summary+6,aim.length);
                        loc += (e_summary+6);
                        continue;
                    }
                    if ( (target3 != "") && (aim2.indexOf(target3) == -1) ) {
                        aim = aim.substring(e_summary+6,aim.length);
                        loc += (e_summary+6);
                        continue;
                    }
                    if ( (target4 != "") && (aim2.indexOf(target4) == -1) ) {
                        aim = aim.substring(e_summary+6,aim.length);
                        loc += (e_summary+6);
                        continue;
                    }
                    if ( (target5 != "") && (aim2.indexOf(target5) == -1) ) {
                        aim = aim.substring(e_summary+6,aim.length);
                        loc += (e_summary+6);
                        continue;
                    }
                    done = true;
                }
                else
                    break;
            }
            else
                break;
        } while ( done == false );

        if ( s_summary > 0 ) {
            loc = inn.substring(0,loc+s_summary).lastIndexOf("<H3>");
            if ( loc >= 0 ) {
                txt = inn.substring(loc+4,inn.length);
                targetHREF  = txt.substring(txt.indexOf("href=\"")+6,txt.indexOf("\" target=_blank")); 
                targetTITLE = getTarget();
                if ( targetHREF != "" )
                    page_url  = targetHREF;
                if ( targetTITLE != "" )
                    title_txt = targetTITLE;
            }
        }
    }
    else if ( page_url.match( "http://www.google.co.jp/search" ) ) {
        /* Google の検索結果を元に、元記事を参照することなくクリップするための処理 */
        /*  -- LostGoogles をインストールしている環境でも動作確認済み              */

        /* cite_html は正規化済み(IEの場合)なのに対し、inn の方はオリジナルの html ソース  */
        /* のままなので、そのまま文字列比較はできないため、比較できる形式に cite_html を変 */
        /* 形する (本当は、DOM を使ってあれこれすべき)                                      */
        while ( cite_html.match( "</FONT>" ) ) {
            f = cite_html.indexOf( "</FONT>" );
            cite_html = cite_html.substring(0,f) + cite_html.substring(f+7,cite_html.length);
        }
        if ( cite_html.match( "\n" ) ) {
            f = cite_html.indexOf( "\n" );
            cite_html = cite_html.substring(0,f-1);
        }

        /*
         * locG       locHREF                                                     locTitle
         * |           |                                                             |
         * v           v                                                             v
         * <p class=g><a href="元記事URL" onmousedown="return clk(this,'res',11,'')">元記事タイトル</a>
         * (検索キーワードと一致する部分は <b> ~ </b> で囲まれている)
         */
        loc = inn.indexOf(cite_html);
        if ( loc >= 0 ) {
            sub = inn.substring(0,loc);
            locG = sub.lastIndexOf("<P class=g>");
            if ( locG >= 0 ) {
                sub = sub.substring(locG+11,loc);
                locHREF = sub.indexOf("href=\"");
                if ( locHREF >= 0 ) {
                    sub = sub.substring(locHREF+6,sub.length);
                    targetHREF  = sub.substring(0,sub.indexOf('"'));
                    targetTITLE = "";
                    locTitle    = sub.indexOf("\">");
                    if ( locTitle >= 0 ) {
                        locTitle += 2;
                        sub = sub.substring(locTitle,sub.length);
                        locEnd = sub.indexOf("</A>");
                        if ( locEnd >= 0 ) {
                            targetTITLE = sub.substring(0,locEnd);

                            /* <B> ~ </B> を除去 */
                            while ( targetTITLE.match( "<B>" ) || targetTITLE.match( "</B>" ) ) {
                                b1 = targetTITLE.indexOf( "<B>" );
                                if ( b1 >= 0 )
                                    targetTITLE = targetTITLE.substring(0,b1) + targetTITLE.substring(b1+3,targetTITLE.length);
                                b2 = targetTITLE.indexOf( "</B>" );
                                if ( b2 >= 0 )
                                    targetTITLE = targetTITLE.substring(0,b2) + targetTITLE.substring(b2+4,targetTITLE.length);
                            }
                        }
                    }
                    if ( targetHREF != "" )
                        page_url  = targetHREF;
                    if ( targetTITLE != "" )
                        title_txt = targetTITLE;
                }
            }
        }
    }
}

if ( title_txt == "[object]" ) {
    /* title に '*' が含まれている場合にうまく取り込めない現象への対策 */
    str = obj.document.body.parentNode;
    inn = str.innerHTML;
    s = inn.indexOf("<TITLE>");
    e = inn.indexOf("</TITLE>");
    if ( (s > 0) && (e > s) )
        title_txt = inn.substring(s + 7, e);
}

page_str = "";
len = page_url.length;
for ( i = 0; i < len; i++ ) {
    if ( page_url.charAt(i) == '&' )
	page_str += "&amp;";
    else
	page_str += page_url.charAt(i);
}

x = prompt("コメント","ノーコメント");
y = prompt("タイトル","モヒカン族に関する言及");

newWin = window.open("http://mohican.g.hatena.ne.jp/bbs/2#edit","mohican");
newWin.document.open();
newWin.document.write('<html>\n');
newWin.document.write('<head>\n');
newWin.document.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n');
newWin.document.write('<meta http-equiv="Content-Style-Type" content="text/css">\n');
newWin.document.write('<meta http-equiv="Content-Script-Type" content="text/javascript">\n');
newWin.document.write('<link rel="stylesheet" href="http://mohican.g.hatena.ne.jp/theme/hatena-black/hatena-black.css" type="text/css" media="all" charset="euc-jp">\n');
newWin.document.write('<title>モヒカン族への言及 投稿画面</title>\n');
newWin.document.write('</head>\n');
newWin.document.write('<body>\n');
newWin.document.write('<div class="hatena-body">\n');
newWin.document.write('<div class="day">\n');
newWin.document.write('<form method="post" action="http://mohican.g.hatena.ne.jp/bbs" name="edit">\n');
newWin.document.write('<input type="hidden" name="mode" value="enter">\n');
newWin.document.write('<input type="hidden" name="tid"  value="2">\n');
newWin.document.write('<h2><span class="title"><input type="text" name="title" size=40 value="' + y + '"></span></h2>\n');
newWin.document.write('<div class="body">\n');
newWin.document.write('<textarea cols="60" rows="20" name="body">' + '-' + title_txt + '\n' + page_str + '\n&gt;&gt;\n' + cite_txt + '\n&lt;&lt;\n' + x + '</textarea><br>\n');
newWin.document.write('<p class="footnote">※はてな記法が使用できます。</p>\n');
newWin.document.write('<input type="submit" value="この内容を書き込む">\n');
newWin.document.write('<input type="checkbox" name="trivial" value="1"><span style="font-size: smaller;">ちょっとした書き込み</span>\n');
newWin.document.write('</div>\n');
newWin.document.write('</form>\n');
newWin.document.write('</div>\n');
newWin.document.write('</div>\n');
newWin.document.write('</body>\n');
newWin.document.write('</html>\n');
newWin.document.close();
newWin.focus();
</script>

それでは、お楽しみください!!


関連ページ: モヒカン族 - tsupoの日記

投稿者: tsupo 2005.07.07 午後 11:44 | 固定リンク | このエントリーをはてなブックマークに追加 | このエントリを del.icio.us に登録 このエントリの del.icio.us での登録状況 | このエントリを Buzzurl に追加このエントリの Buzzurl での登録状況 | このエントリをlivedoorクリップに登録 このエントリのlivedoorクリップでの登録状況 このエントリをlivedoorクリップに登録している人の数 | 酢鶏巡回中

楽天市場


品揃え豊富で安い!NTT-X Store


アマゾンわくわく探検隊

トラックバック

この記事のトラックバックURL:

この記事へのトラックバック一覧です: 右クリックで「モヒカン族」:

» 右クリックで「モヒカン族」 (改訂版) from 観測気球
はてなグループの掲示板もCSRF対策が実施されました。それに伴い、以前の『右クリックで「モヒカン族」』は期待通りに動かなくなってしまったため、改訂版を用意しました。以前の版をお使いの方は、差し替えをお願いします。... 続きを読む

受信: 2005.07.21 午前 11:58

コメント


モヒカンは峰先生でしょうきっと
モヒカンだね

投稿者: 酢鶏@人工無能 (2005.07.09 午前 07:44)

ワード

ニッセン

fujisan.co.jp

楽天市場