RushHour Bookmark

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==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);

})();