atlassian-tweaks icon indicating copy to clipboard operation
atlassian-tweaks copied to clipboard

Confluence: add anchor link to all headings

Open Finkregh opened this issue 6 months ago • 3 comments

Feel free to add this to your collection:

// ==UserScript==
// @name         Confluence heading linker
// @version      0.0.1
// @description  Add permalink to conflucence headings
// @author       FIXME
// @match        https://FIXME/wiki/display/*
// @grant        GM_log
// @license      MIT

// ==/UserScript==

(function() {
    'use strict';

    window.addEventListener('load', function () {
        const headings = document.querySelectorAll('h1, h2, h3, h4, h5, h6');
        const addedElements = [];

        headings.forEach(heading => {
            // Check if the heading already contains a link
            if (!heading.querySelector('a[href^="#"]')) {
                const id = heading.id;
                const space_span = document.createElement('span');
                space_span.textContent = ' ';
                const a = document.createElement('a');
                a.href = `#${id}`;
                a.textContent = '#';
                heading.appendChild(space_span);
                addedElements.push(space_span);
                heading.appendChild(a);
                addedElements.push(a);
                GM_log(`Added link to heading ${heading.textContent.replace(/\s+/g, ' ')}`);
            }
        });

        const editPageLink = document.getElementById('editPageLink');
        if (editPageLink) {
            editPageLink.addEventListener('click', function() {
                addedElements.forEach(link => {
                    link.remove();
                });
                GM_log('Removed all added links');
            });
        }
    });
})();

Finkregh avatar Aug 15 '24 09:08 Finkregh