Wiki Link Preview

Show a preview of a linked wiki page when hovering over a link like Wikipedia does.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           Wiki Link Preview
// @namespace      https://github.com/AbdurazaaqMohammed
// @version        1.0
// @author         Abdurazaaq Mohammed
// @description    Show a preview of a linked wiki page when hovering over a link like Wikipedia does.
// @match          https://*.*/*
// @match          http://*.*/*
// @homepage       https://github.com/AbdurazaaqMohammed/userscripts
// @license        The Unlicense
// @supportURL     https://github.com/AbdurazaaqMohammed/userscripts/issues
// @grant          GM_xmlhttpRequest
// @connect        *
// ==/UserScript==
(function() {
	'use strict';

	const previewBox = document.createElement('div');
	previewBox.id = 'previewBox';
	Object.assign(previewBox.style, {
		position: 'absolute',
		border: '1px solid #ccc',
		padding: '10px',
		background: '#fff',
		zIndex: 10000,
		display: 'none',
		maxWidth: '300px',
		boxShadow: '0 4px 8px rgba(0,0,0,0.1)',
		borderRadius: '4px'
	});
	document.body.appendChild(previewBox);

	function extractSummary(htmlContent, isWikiLink) {
		const tempDiv = document.createElement('div');
		tempDiv.innerHTML = htmlContent;
		const summary = tempDiv.querySelector(isWikiLink ? '#mw-content-text .mw-parser-output > p' : 'p') ;
		return summary ? summary.textContent : 'No summary available.';
	}

	function showPreviewBox(link, content) {
		previewBox.innerHTML = content;
		previewBox.style.display = 'block';
		const rect = link.getBoundingClientRect();
		previewBox.style.top = (window.scrollY + rect.top - previewBox.offsetHeight - 10) + 'px';
		previewBox.style.left = (window.scrollX + rect.left) + 'px';
	}

	document.body.addEventListener('mouseover', function(event) {
		const t = event.target;
		if (t.tagName === 'A') {
			const url = t.href;
		  const isWikiLink = url.includes('wiki') || url.includes('miraheze');
			if (url
				&& isWikiLink // Comment this line if you want to try it with all links.
			   ) {
				GM_xmlhttpRequest({
					method: 'GET',
					url: url,
					onload: function(response) {
						try {
							const summary = extractSummary(response.responseText, isWikiLink);
							showPreviewBox(t, summary);
						} catch (e) {
							console.error('Failed to extract summary', e);
							showPreviewBox(t, 'Error loading summary.');
						}
					},
					onerror: function(error) {
						console.error('Error loading the page:', error);
						showPreviewBox(t, 'Error loading page.');
					}
				});
			}
		}
	});

	document.body.addEventListener('mouseout', function(event) {
		if (event.target.tagName === 'A') {
			previewBox.style.display = 'none';
		}
	});
})();