RushHour Bookmark

ページのクリックしたリスト要素(ul.list1)に枠(border)を追加しxpathを保存・復元時にスクロール

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         RushHour Bookmark
// @namespace    tampermonkey.net
// @version      2.1
// @description  ページのクリックしたリスト要素(ul.list1)に枠(border)を追加しxpathを保存・復元時にスクロール
// @author       Toshiaki
// @match        https://wikiwiki.jp/ali_may/%E3%83%A9%E3%83%83%E3%82%B7%E3%83%A5%E3%82%A2%E3%83%AF%E3%83%BC/%E5%9C%B0%E4%B8%8B%E9%A7%90%E8%BB%8A%E5%A0%B4
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_registerMenuCommand
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

(function() {
	'use strict';

	const key = 'ali_may_'+location.pathname;
	GM_registerMenuCommand('delete Bookmark', () => {
		GM_deleteValue(key);
	});

	window.addEventListener('load', () => {
		const xpath = GM_getValue(key, '');
		if (xpath) {
			console.log(`xpath(${xpath})`);
			const result = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
			if (result.snapshotLength > 0) {
				const firstElement = result.snapshotItem(0);
				updateList(firstElement);
				firstElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest'});
				//console.log(firstElement.textContent);
			} else {
				console.log(`xpath(${xpath}) element not found.`);
			}
		}
	});

	const getXPath = (element) => {
		if (element.id !== '') {
			return `id("${element.id}")`;
		}
		if (element === document.body) {
			return '/html/' + element.tagName.toLowerCase();
		}
		let ix = 0;
		let siblings = element.parentNode.childNodes;
		for (let i = 0; i < siblings.length; i++) {
			let sibling = siblings[i];
			if (sibling === element) {
				return getXPath(element.parentNode) + '/' + element.tagName.toLowerCase() + '[' + (ix + 1) + ']';
			}
			if (sibling.nodeType === 1 && sibling.tagName === element.tagName) {
				ix++;
			}
		}
	}
	let ul = null;
	const updateList = (element) => {
		if (element) {//remove border add border
			if (ul) {
				ul.style.outline = '';
				ul.style.outlineOffset = '';
			}
			ul = element;
			ul.style.outline = '4px solid #111111';
			ul.style.outlineOffset = '2px';
			GM_setValue(key, getXPath(ul));
		}
	}
	document.addEventListener('click', (e) => {
		updateList(e.target.closest('ul.list1'));
	}, true);

})();