🏠 Home 

Wiki Link Preview

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

  1. // ==UserScript==
  2. // @name Wiki Link Preview
  3. // @namespace https://github.com/AbdurazaaqMohammed
  4. // @version 1.0
  5. // @author Abdurazaaq Mohammed
  6. // @description Show a preview of a linked wiki page when hovering over a link like Wikipedia does.
  7. // @match https://*.*/*
  8. // @match http://*.*/*
  9. // @homepage https://github.com/AbdurazaaqMohammed/userscripts
  10. // @license The Unlicense
  11. // @supportURL https://github.com/AbdurazaaqMohammed/userscripts/issues
  12. // @grant GM_xmlhttpRequest
  13. // @connect *
  14. // ==/UserScript==
  15. (function() {
  16. 'use strict';
  17. const previewBox = document.createElement('div');
  18. previewBox.id = 'previewBox';
  19. Object.assign(previewBox.style, {
  20. position: 'absolute',
  21. border: '1px solid #ccc',
  22. padding: '10px',
  23. background: '#fff',
  24. zIndex: 10000,
  25. display: 'none',
  26. maxWidth: '300px',
  27. boxShadow: '0 4px 8px rgba(0,0,0,0.1)',
  28. borderRadius: '4px'
  29. });
  30. document.body.appendChild(previewBox);
  31. function extractSummary(htmlContent, isWikiLink) {
  32. const tempDiv = document.createElement('div');
  33. tempDiv.innerHTML = htmlContent;
  34. const summary = tempDiv.querySelector(isWikiLink ? '#mw-content-text .mw-parser-output > p' : 'p') ;
  35. return summary ? summary.textContent : 'No summary available.';
  36. }
  37. function showPreviewBox(link, content) {
  38. previewBox.innerHTML = content;
  39. previewBox.style.display = 'block';
  40. const rect = link.getBoundingClientRect();
  41. previewBox.style.top = (window.scrollY + rect.top - previewBox.offsetHeight - 10) + 'px';
  42. previewBox.style.left = (window.scrollX + rect.left) + 'px';
  43. }
  44. document.body.addEventListener('mouseover', function(event) {
  45. const t = event.target;
  46. if (t.tagName === 'A') {
  47. const url = t.href;
  48. const isWikiLink = url.includes('wiki') || url.includes('miraheze');
  49. if (url
  50. && isWikiLink // Comment this line if you want to try it with all links.
  51. ) {
  52. GM_xmlhttpRequest({
  53. method: 'GET',
  54. url: url,
  55. onload: function(response) {
  56. try {
  57. const summary = extractSummary(response.responseText, isWikiLink);
  58. showPreviewBox(t, summary);
  59. } catch (e) {
  60. console.error('Failed to extract summary', e);
  61. showPreviewBox(t, 'Error loading summary.');
  62. }
  63. },
  64. onerror: function(error) {
  65. console.error('Error loading the page:', error);
  66. showPreviewBox(t, 'Error loading page.');
  67. }
  68. });
  69. }
  70. }
  71. });
  72. document.body.addEventListener('mouseout', function(event) {
  73. if (event.target.tagName === 'A') {
  74. previewBox.style.display = 'none';
  75. }
  76. });
  77. })();