選択した文字列をWikipediaで検索する、リンクを作成するUserScript
選択した文字列をWikipedia で検索する
/yutaro/google search -- 元ネタはこちら
検索のとき、ブラケット[]は削除される
文字列が半角英数字のみ場合は、英語と判断して、英語版Wikipediaを検索する
code:script.js
scrapbox.PopupMenu.addButton({
title: 'Ws',
onClick: text => {
if( text.match(/^\[\S*\]$/g) ){
text = text.replace(/\[/g, '');
} else {
text = text.replace(/\[\S*\s/g, '');
}
text = text.replace(/\]/g, '').replace(/^\s+/, '');
const uri = encodeURI(text.replace(' ', '+'));
const regexp = /^[a-zA-Z0-9!-/:-@\-`{-~\s*$/gm
if (regexp.test(text)) {
window.open(https://en.wikipedia.org/w/index.php?search=${uri});
} else {
window.open(https://ja.wikipedia.org/w/index.php?search=${uri});
}
}
})
選択した文字列を Wikipediaへの外部リンクの記法に変換する
Wikipediaへの外部リンクの記法の場合は、外部リンクの記法を消す
半角英数字のみ場合は、英語と判断して、英語版Wikipediaに、それ以外は日本語版Wikipediaへのリンクになる
code:script.js
scrapbox.PopupMenu.addButton({
title: 'Wl',
onClick: text => {
const regexp = /^[a-zA-Z0-9!-/:-@\-`{-~\s*$/gm
if (regexp.test(text)) {
const regexp_en = /\https\:\/\/en\.wikipedia\.org\/wiki\/(.+)\s(.+)\/
if (regexp_en.test(text)){
// ${text} → ${text} への変換
return text.replace(regexp_en, "$2")
} else {
return [https://en.wikipedia.org/wiki/${encodeURIComponent(text)} ${text}]
}
} else {
const regexp_ja = /\https\:\/\/ja\.wikipedia\.org\/wiki\/(.+)\s(.+)\/
if (regexp_ja.test(text)){
// ${text} → ${text} への変換
return text.replace(regexp_ja, "$2")
} else {
return [https://ja.wikipedia.org/wiki/${encodeURIComponent(text)} ${text}]
}
}
}
})
#UserScript
#正規表現