Wiki Link Preview

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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';
		}
	});
})();