LoginSignup
3
1

More than 3 years have passed since last update.

Qiita から Wikipedia 検索するブックマークレット

Last updated at Posted at 2017-07-29

Qiita の新デザインに伴い、2017/12/07現在このブックマークレットは動作しません。善処します。
2020/02/10 現在、新デザインには対応したのですが、このブックマークレットは動作しません。善処します。SimpleAPI が CrossOrigin のヘッダを返さない仕様のため CORS のエラーで取得できないからです。

スクリーンショット 2017-07-29 20.21.07.png

TL;DR

@yyano さん発案@hidao さんパッチ提供の、Qiitaタグ選択時の一覧でブックマークを選択するとWikipediaの検索結果が表示されるブックマークレットです。
また、文中の単語を選んでブックマークを選択してもWikipediaの概要が表示されます。(上図参照)

※「Mac で単語を選択して強く押し込んだ時と同じ機能だよ」Σ( ̄□ ̄;)

Wiqiita_v20200210-1242.js
javascript:(function(){console.log("-".repeat(35));console.log(" Qiita Bookmarklet for Wikipedia Search");console.log(" Ver. 20200210-1242");console.log(" Latest ver see: %o","https://paiza.io/gists/bce82fc1e19c0303549c1d018ad4b592");console.log("-".repeat(35));function camel2spsep(str){var REGEXP_ONLYFIRST="[A-Z][a-z]";var REGEXP_REPTDCAPT="[A-Z]+$";separator="_";console.log("Camel to space: %s",str);return str.replace(RegExp(REGEXP_ONLYFIRST,'g'),function(match){return separator+match}).replace(RegExp(REGEXP_REPTDCAPT,'g'),function(match){return separator+match}).trim()};function setStyle_Wiki(obj){obj.style.borderStyle="solid";obj.style.borderColor="rgb(78,163,10)";obj.style.marginBottom="1em";obj.style.padding="0.5em";obj.style.fontSize="1em";obj.style.textAlign="justify";obj.style.textJustify="inter-ideograph"}function getUrlWiki(keyword,callback){keyword=encodeURIComponent(keyword);console.log('URL created with keyword: %s',keyword);return "https://wikipedia.simpleapi.net/api?"+"keyword="+keyword+"&output=json&callback="+callback}function pushJSON(url){console.log('Requesting JSONP to URL: %o',url);var loadJson=document.createElement('script');if(url=="final"){loadJson.innerText="getJSON();"}else{loadJson.src=url}loadJson.type='text/javascript';document.head.appendChild(loadJson)};var keyword_raw='';if(window.navigator.appName=='Microsoft Internet Explorer'){keyword_raw=document.selection.createRange().text}else{keyword_raw=window.getSelection().toString()}if(!keyword_raw||keyword_raw==''){keyword_raw=decodeURIComponent(location.pathname.replace('/tags/',''))}var wikiArea=document.getElementById("wikipedia-wrapper");if(!wikiArea){wikiArea=document.createElement("div");wikiArea.id='wikipedia-wrapper';wikiArea.innerHTML="<div id='wikipedia-title'></div><div id='wikipedia-body'></div>";var snsArea=document.getElementsByClassName("it-Toc_nav");snsArea[0].parentNode.insertBefore(wikiArea,snsArea[0])}var wikiTitle=document.getElementById('wikipedia-title');var wikiBody=document.getElementById('wikipedia-body');var keyword_enc=encodeURIComponent(keyword_raw);var keyword_uncml=camel2spsep(keyword_raw);var keyword_captl=keyword_raw.charAt(0).toUpperCase()+keyword_raw.slice(1);setStyle_Wiki(wikiArea);wikiTitle.style.fontWeight="bold";wikiTitle.style.paddingBottom="0.5em";wikiTitle.innerHTML="検索用語:<span id='wikipedia-keyword'>"+keyword_raw+"</span>";wikiBody.innerText="データを読み込み中...";var getJsonElement=document.createElement('script');getJsonElement.innerText="var isResultFound=!1;function getJSON(json){console.log(JSON.stringify(json,undefined,4));console.table(json);var wikiArea=document.getElementById(\"wikipedia-wrapper\");if(wikiArea){var wikiTitle=document.getElementById(\"wikipedia-title\");var wikiBody=document.getElementById(\"wikipedia-body\");var wikiKeyword=document.getElementById(\"wikipedia-keyword\");var keyword_raw=wikiKeyword.innerText;var keyword_enc=encodeURIComponent(keyword_raw);if(json){console.log(\"Search result found: %s\",json[0].title);wikiTitle.innerHTML=\"<div id='wikipedia-keyword'>\"+json[0].title+\"<\/div>\";var msg_Hit='';var wikiContent=json[0].body.split('\u3002').join('\u3002<br>');msg_Hit+=\"<div id='wikipedia-result'>\";msg_Hit+=\"<div id='wikipedia-content'>\"+wikiContent+'<\/div>';msg_Hit+=\"<div>[<a href='https:\/\/ja.wikipedia.org\/w\/index.php?search=\"+keyword_enc+\"' target=_blank>\"+\"Wikipedia\u672c\u5bb6\u3067\"+keyword_raw+\"\u3092\u518d\u691c\u7d22\u3059\u308b<\/a>...]<\/div>\";msg_Hit+=\"<div>[<a href='https:\/\/eow.alc.co.jp\/\"+keyword_enc+\"' target=_alc>\"+keyword_raw+\"\u3092ALC\u3067\u8abf\u3079\u308b<\/a>...]<\/div>\";msg_Hit+=\"<div>[<a href='http:\/\/qiita.com\/search?q=\"+keyword_enc+\"' target=_qiita>\"+keyword_raw+\"\u3092Qiita\u3067\u8abf\u3079\u308b<\/a>]<\/div>\";msg_Hit+=\"<\/div>\";wikiBody.innerHTML=msg_Hit}else{console.log(\"Search result NOT found: %s\",keyword_raw);var msg_NoHit='';msg_NoHit+=\"<div id='wikipedia-result'>\";msg_NoHit+=\"<div>WikipediaAPI\u3067\u306f\u3001\u3053\u306e\u7528\u8a9e\u306f\u898b\u3064\u304b\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/div>\";msg_NoHit+=\"<div>[<a href='https:\/\/ja.wikipedia.org\/w\/index.php?search=\"+keyword_enc+\"' target=_wikipedia>\"+keyword_raw+\"\u3092Wikipedia\u672c\u5bb6\u3067\u8abf\u3079\u308b<\/a>...]<\/div>\";msg_NoHit+=\"<div>[<a href='https:\/\/eow.alc.co.jp\/\"+keyword_enc+\"' target=_alc>\"+keyword_raw+\"\u3092ALC\u3067\u8abf\u3079\u308b<\/a>...]<\/div>\";msg_NoHit+=\"<div>[<a href='http:\/\/qiita.com\/search?q=\"+keyword_enc+\"' target=_qiita>\"+keyword_raw+\"\u3092Qiita\u3067\u8abf\u3079\u308b<\/a>]<\/div>\";msg_NoHit+=\"<\/div>\";wikiBody.innerHTML=msg_NoHit}}else{console.log(\"No wiki area created\")}};function getJSON_raw(json){if(json&&!isResultFound){console.log(\"Got a match with raw keyword.\");getJSON(json);isResultFound=!0}else if(json&&isResultFound){console.log('Got a match with raw keyword but did not push. Bool is true.')}else{console.log(\"No match with raw keyword.\")}}function getJSON_enc(json){if(json&&!isResultFound){console.log(\"Got a match with enc keyword.\");getJSON(json);isResultFound=!0}else if(json&&isResultFound){console.log('Got a match with enc keyword but did not push. Bool is true.')}else{console.log(\"No match with enc keyword.\")}}function getJSON_uncml(json){if(json&&!isResultFound){console.log(\"Got a match with uncml keyword.\");getJSON(json);isResultFound=!0}else if(json&&isResultFound){console.log('Got a match with uncml keyword but did not push. Bool is true.')}else{console.log(\"No match with uncml keyword.\")}}function getJSON_captl(json){if(json&&!isResultFound){console.log(\"Got a match with captl keyword.\");getJSON(json);isResultFound=!0}else if(json&&isResultFound){console.log('Got a match with captl keyword but did not push. Bool is true.')}else{console.log(\"No match with captl keyword.\")}}";document.head.appendChild(getJsonElement);var url_api_wiki_raw=getUrlWiki(keyword_raw,"getJSON_raw");var url_api_wiki_enc=getUrlWiki(keyword_enc,"getJSON_enc");var url_api_wiki_uncml=getUrlWiki(keyword_uncml,"getJSON_uncml");var url_api_wiki_captl=getUrlWiki(keyword_captl,"getJSON_captl");pushJSON(url_api_wiki_raw);pushJSON(url_api_wiki_enc);pushJSON(url_api_wiki_uncml);pushJSON(url_api_wiki_captl);pushJSON("final")})();

最新版とカスタム

上記 Paiza.io で最新のブックマークレットが取得できます。また、このPaiza.io上でいじれば自分好みにカスタムできます。

マスターの Gist
はこちら: https://gist.github.com/KEINOS/bce82fc1e19c0303549c1d018ad4b592

マスターへの反映・提案・要望は Qiitadon で「#Wiqiita」ハッシュタグをつけてトゥートしてください。

動作環境

横着なので Mac 版 Chrome(59.0.3071.115, Official Build,64bit) でしか検証していません。

3
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1