User:Polygnotus/Scripts/SectionLinks.js: Difference between revisions

Content deleted Content added
Created page with '// Add link icons to h2 headers for current versions, old revisions, and diffs (function() { function addLinkIcons() { const headers = document.querySelectorAll('h2[id]'); headers.forEach(header => { const isCurrentVersion = !window.___location.search.match(/[?&]oldid=(\d+)/) && !document.querySelector('.diff-currentversion-title'); if (isCurrentVersion) { // Add two icons for cu...'
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1:
// Add link icons with tooltips to h2 headers for current versions, old revisions, and diffs
(function() {
function addLinkIcons() {
if (mw.config.get('wgIsMainPage')) {
return;
}
const headers = document.querySelectorAll('h2[id]');
headers.forEach(header => {
// Skip the table of contents heading
if (header.id === 'mw-toc-heading') {
return;
}
 
const isCurrentVersion = !window.___location.search.match(/[?&]oldid=(\d+)/) && !document.querySelector('.diff-currentversion-title');
if (isCurrentVersion) {
// Add two icons for current version
addIcon(header, '🔗', false, 'Copy link to this section'); // Chain link emoji for section link
addIcon(header, '📌', true, 'Copy permalink to this section'); // Pushpin emoji for permalink
} else {
// Add single icon for old revision or diff
addIcon(header, '�', true, 'Copy link to this section of this specific revision');
}
});
}
 
function addIcon(header, iconText, isPermalink, tooltipText) {
const icon = document.createElement('span');
icon.innerHTML = iconText;
Line 24 ⟶ 34:
icon.style.marginRight = '5px';
icon.style.fontSize = '0.8em';
icon.title = tooltipText; // Add tooltip
icon.addEventListener('click', function(e) {