Last active
February 10, 2020 03:42
-
-
Save KEINOS/bce82fc1e19c0303549c1d018ad4b592 to your computer and use it in GitHub Desktop.
Wiqiitaブックマークレット・ジェネレーター:Qiita上で単語を選択、もしくはQiitaタグ選択時の一覧画面でブックマークを選ぶとWikipediaの検索結果が表示されるブックマークレット用Javascriptを出力するPHPです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$script1 =<<<HEREDOC | |
var isResultFound = false; | |
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( '。' ).join( '。<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本家で"+ keyword_raw + "を再検索する</a>...]</div>"; | |
msg_Hit += "<div>[<a href='https://eow.alc.co.jp/" | |
+ keyword_enc + "' target=_alc>" | |
+ keyword_raw + "をALCで調べる</a>...]</div>"; | |
msg_Hit += "<div>[<a href='http://qiita.com/search?q=" | |
+ keyword_enc + "' target=_qiita>" | |
+ keyword_raw + "をQiitaで調べる</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では、この用語は見つかりませんでした。</div>"; | |
msg_NoHit += "<div>[<a href='https://ja.wikipedia.org/w/index.php?search=" | |
+ keyword_enc + "' target=_wikipedia>" | |
+ keyword_raw + "をWikipedia本家で調べる</a>...]</div>"; | |
msg_NoHit += "<div>[<a href='https://eow.alc.co.jp/" | |
+ keyword_enc + "' target=_alc>" | |
+ keyword_raw + "をALCで調べる</a>...]</div>"; | |
msg_NoHit += "<div>[<a href='http://qiita.com/search?q=" | |
+ keyword_enc + "' target=_qiita>" | |
+ keyword_raw + "をQiitaで調べる</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 = true; | |
} 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 = true; | |
} 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 = true; | |
} 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 = true; | |
} 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."); | |
} | |
} | |
HEREDOC; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$script2 =<<<HEREDOC | |
javascript: (function () { | |
console.log("-".repeat(35)); | |
console.log(" Qiita Bookmarklet for Wikipedia Search"); | |
console.log(" Ver. ${version}"); | |
console.log(" Latest ver see: %o", "${url_original}"); | |
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 = "${script1}"; | |
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"); | |
})(); | |
HEREDOC; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Content-type: text/text; charset=utf-8"); | |
// constants | |
define('MINIFY_STRING', '"(?:[^"\\\]|\\\.)*"|\'(?:[^\'\\\]|\\\.)*\''); | |
define('MINIFY_COMMENT_CSS', '/\*[\s\S]*?\*/'); | |
define('MINIFY_COMMENT_HTML', '<!\-{2}[\s\S]*?\-{2}>'); | |
define('MINIFY_COMMENT_JS', '//[^\n]*'); | |
define('MINIFY_PATTERN_JS', '/[^\n]+?/[gimuy]*'); | |
define('MINIFY_HTML', '<[!/]?[a-zA-Z\d:.-]+[\s\S]*?>'); | |
define('MINIFY_HTML_ENT', '&(?:[a-zA-Z\d]+|\#\d+|\#x[a-fA-F\d]+);'); | |
define('MINIFY_HTML_KEEP', '<pre(?:\s[^<>]*?)?>[\s\S]*?</pre>|<code(?:\s[^<>]*?)?>[\s\S]*?</code>|<script(?:\s[^<>]*?)?>[\s\S]*?</script>|<style(?:\s[^<>]*?)?>[\s\S]*?</style>|<textarea(?:\s[^<>]*?)?>[\s\S]*?</textarea>'); | |
// normalize line–break(s) | |
function n($s) { | |
return str_replace(["\r\n", "\r"], "\n", $s); | |
} | |
// trim once | |
function t($a, $b) { | |
if ($a && strpos($a, $b) === 0 && substr($a, -strlen($b)) === $b) { | |
return substr(substr($a, strlen($b)), 0, -strlen($b)); | |
} | |
return $a; | |
} | |
function fn_minify($pattern, $input) { | |
return preg_split('#(' . implode('|', $pattern) . ')#', $input, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); | |
} | |
function fn_minify_js($input, $comment = 2, $quote = 2) { | |
if (!is_string($input) || !$input = n(trim($input))) return $input; | |
$output = $prev = ""; | |
foreach (fn_minify([MINIFY_COMMENT_CSS, MINIFY_STRING, MINIFY_COMMENT_JS, MINIFY_PATTERN_JS], $input) as $part) { | |
if (trim($part) === "") continue; | |
if ($comment !== 1 && ( | |
strpos($part, '//') === 0 || // Remove inline comment(s) | |
strpos($part, '/*') === 0 && substr($part, -2) === '*/' | |
)) { | |
if ( | |
$comment === 2 && ( | |
// Detect special comment(s) from the third character. It should be a `!` or `*` → `/*! keep */` or `/** keep */` | |
strpos('*!', $part[2]) !== false || | |
// Detect license comment(s) from the content. It should contains character(s) like `@license` | |
stripos($part, '@licence') !== false || // noun | |
stripos($part, '@license') !== false || // verb | |
stripos($part, '@preserve') !== false | |
) | |
) { | |
$output .= $part; | |
} | |
continue; | |
} | |
if ($part[0] === '/' && (substr($part, -1) === '/' || preg_match('#\/[gimuy]*$#', $part))) { | |
} else if ($part[0] === '"' && substr($part, -1) === '"' || $part[0] === "'" && substr($part, -1) === "'") { | |
// TODO: Remove quote(s) where possible … | |
$output .= $part; | |
} else { | |
$output .= fn_minify_js_union($part); | |
} | |
$prev = $part; | |
} | |
return trim($output); | |
} | |
function fn_minify_js_union($input) { | |
return preg_replace([ | |
// Remove white–space(s) around punctuation(s) [^1] | |
'#\s*([!%&*\(\)\-=+\[\]\{\}|;:,.<>?\/])\s*#', | |
// Remove the last semi–colon and comma [^2] | |
'#[;,]([\]\}])#', | |
// Replace `true` with `!0` and `false` with `!1` [^3] | |
'#\btrue\b#', '#\bfalse\b#', '#\b(return\s?)\s*\b#', | |
// Replace `new Array(x)` with `[x]` … [^4] | |
'#\b(?:new\s+)?Array\((.*?)\)#', '#\b(?:new\s+)?Object\((.*?)\)#' | |
], [ | |
// [^1] | |
'$1', | |
// [^2] | |
'$1', | |
// [^3] | |
'!0', '!1', '$1', | |
// [^4] | |
'[$1]', '{$1}' | |
], $input); | |
} | |
// function to escape javascript in a javascript code. | |
function fm_escape_js_str($str) { | |
return substr(json_encode($str), 1, -1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//////////////////////////////////////////////////////////////////// | |
// Qiitaページの拡張ブックマークレット・ジェネレーター | |
// | |
// Qiitaページ内の選択用語、もしくはQiitaタグ選択時の一覧のタグ用語 | |
// をWikipediaAPI(SimpleAPI経由)で検索して、結果を表示するだけのブッ | |
// クマークレットです。 | |
// | |
// 使い方: | |
// 1) Chromeで適当なページをブックマークする。 | |
// 2) ブックマークを編集してURL部分を下記の出力欄のスクリプトと置き換える。 | |
// 3) ブックメーク名はわかりやすいもの(「Wiqiita検索」など)にします。 | |
// 4-1) Qiitaのページで、調べたい単語を選んでブックマークを選ぶ | |
// 4-2) Qiitaタグ一覧でブックマークを選ぶ | |
// | |
// カスタム: | |
// ヒアドキュメント`EOL`の間がJavascriptコードです。適当に修正して | |
// 「実行」(Ctl-Enter)すると出力結果に minimize 化されたブックマー | |
// クレット用のスクリプトが表示されます。 | |
// 注意: | |
// javascript文内にはコメントを入れないでね。改行が入ってしまいます。 | |
// まぁそれでも動きますがコピペが面倒になります。 | |
// | |
include_once('functions.php'); | |
$version = "20200210-1242"; | |
$url_original = "https://paiza.io/gists/bce82fc1e19c0303549c1d018ad4b592"; | |
// WikipediaAPI から Callback される JSONP の Javascript | |
include_once('bookmarklet_inner.js'); | |
//minimize化 | |
$script1 = fn_minify_js($script1); | |
//ブックマークレット本体内で変数として渡すjavascriptコードなのでエスケープ | |
$script1 = fm_escape_js_str($script1); | |
// ブックマークレット本体 | |
// camel2spsep -> キャメルケース文字の場合にスペースを大文字の前に入れて検索率をアップ | |
// setStyle_Wiki -> Wikipedia要素にCSSを適用する | |
// getUrlWiki -> WikipediaAPIのリクエストURLを作成する | |
// pushJSON -> リクエストURLを渡した結果(JSONP)をヘッダーに格納する | |
include_once('bookmarklet_outer.js'); | |
// echo minifyed javascript | |
echo fn_minify_js($script2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment