Repository navigation
Expand file tree
/
Copy pathheader-links.user.js
More file actions
54 lines (46 loc) · 1.59 KB
/
Copy pathheader-links.user.js
File metadata and controls
54 lines (46 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// ==UserScript==
// @name FlatMMO Wiki Header Links
// @namespace Violentmonkey Scripts
// @match https://flatmmo.wiki/index.php/*
// @grant none
// @version 1.0
// @author -
// @description Adds anchor links to headers on wiki pages
// ==/UserScript==
(function() {
'use strict';
// Find all headers (h1-h6)
const headers = document.querySelectorAll('h1, h2, h3, h4, h5, h6');
headers.forEach(header => {
// Get text content
const text = header.textContent.trim();
if (!text) return;
// Check if header already has an ID, or create one
let id = header.id;
if (!id) {
// Create a URL-safe ID from the text
id = text.replace(/\s+/g, '_').replace(/[^a-zA-Z0-9_]/g, '');
header.id = id;
}
// Skip if header already has a link with this href (avoid duplicates)
if (header.querySelector(`a[href="#${id}"]`)) return;
// Create anchor link
const link = document.createElement('a');
link.href = `#${id}`;
link.textContent = '#';
link.style.marginLeft = '5px';
link.style.textDecoration = 'none';
link.style.color = '#999';
link.style.fontSize = '0.9em';
link.title = `Link to ${text}`;
// Add hover effect
link.addEventListener('mouseenter', () => {
link.style.color = '#0645ad';
});
link.addEventListener('mouseleave', () => {
link.style.color = '#999';
});
// Add link to header
header.appendChild(link);
});
})();