Last active
September 27, 2018 09:47
-
-
Save Getaji/d35788dfe0de9c328d69cfbb6fc655f5 to your computer and use it in GitHub Desktop.
PukiWiki検索一致ページ列挙&自動遷移UserScript
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
// ==UserScript== | |
// @name PukiWiki検索一致ページ列挙&自動遷移 | |
// @namespace https://twitter.com/Getaji | |
// @version 0.1 | |
// @description PukiWiki検索画面でページタイトルにマッチしたページのリスト表示を追加 | |
// @author Getaji | |
// @match *://*.wikiru.jp/index.php?cmd=search* | |
// @match *://wikiwiki.jp/*/?cmd=search* | |
// @grant none | |
// ==/UserScript== | |
// == 使い方 == | |
// - Chrome拡張機能のTampermonkeyで動作確認しています。適当に突っ込んでください。 | |
// - URLはwikiruとwikiwikiを登録しています。必要に応じて追加してください。 | |
// - 必要に応じて設定を変更してください。自動遷移はデフォルトでオフになっています。 | |
// - 開発者向け: 変数命名規則がボロボロなのは許して | |
(function() { | |
'use strict'; | |
// 未定義警告対策 | |
const $ = window.$; | |
// == 設定 ================================================================ | |
// ページタイトルのマッチング | |
// 部分一致: partial | |
// 完全一致: perfect | |
// 前方一致: foward | |
// 後方一致: backward | |
const MATCHING_TYPE = 'partial'; | |
// 最初にマッチしたページに移動 (true / false) | |
// 併せてマッチング方法に完全一致 (perfect) を指定することを推奨 | |
const JUMP_TO_FIRST_MATCHING_PAGE = false; | |
// 複数ワードでの検索時、全てのワードに一致したページのみを列挙する (true / false) | |
const LISTUP_AND = false; | |
// == 処理 =============================================================== | |
const STR_MATCHER = { | |
partial: ((s1, s2) => s1.includes(s2)), | |
perfect: ((s1, s2) => s1 === s2), | |
foward: ((s1, s2) => s1.startsWith(s2)), | |
backward: ((s1, s2) => s1.endsWith(s2)) | |
}[MATCHING_TYPE]; | |
const MATCHING_TYPE_STR = { | |
partial: '部分一致', | |
perfect: '完全一致', | |
foward: '前方一致', | |
backward: '後方一致' | |
}[MATCHING_TYPE]; | |
const allLi = $('#body li'); | |
if (allLi.length === 0) return; | |
const sources = $('.small strong').toArray().map(el => el.textContent.trim()); | |
const pageMatcher = ( | |
LISTUP_AND ? (title) => { | |
for (const s of sources) { | |
if (!STR_MATCHER(title, s)) { | |
return false; | |
} | |
} | |
return true; | |
} : (title) => { | |
for (const s of sources) { | |
if (STR_MATCHER(title, s)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
); | |
const matchedLists = allLi.toArray().filter(el => { | |
return pageMatcher(el.textContent.trim()); | |
}); | |
if (JUMP_TO_FIRST_MATCHING_PAGE && matchedLists.length > 0) { | |
location.assign(matchedLists[0].querySelector('a').href); | |
} | |
const ul = $('#body ul'); | |
const container = $(`<div>タイトルにマッチするページ(${MATCHING_TYPE_STR} / ${LISTUP_AND ? 'AND' : 'OR'}) ${matchedLists.length}件:</div>`).insertBefore(ul); | |
const matchedUl = $('<ul/>').appendTo(container); | |
ul.before('<hr class="full_hr">'); | |
const matchedListsHTML = matchedLists.map(el => { | |
return el.outerHTML; | |
}).join(''); | |
matchedUl.html(matchedListsHTML); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment