MediaWiki:Gadget-Anchor.js

From Linguifex
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// <nowiki>
// Preferences ([[MediaWiki:Gadget-WiktGadgetPrefs.js]])
const pref = mw.wiktGadgetPrefs.get("gadget-anchor", {"label": {"en": "Anchor"}},
	{
		"linkType": {
			"type": "strenum",
			"default": "fullUrl",
			"label": {"en": "How the link should be copied"},
			"choices": ["fullUrl", "wikilink", "template"],
			"choiceLabels": {
				"en": {
					"fullUrl": "Full URL (e.g. https://en.wiktionary.org/wiki/bee#English)",
					"wikilink": "Wikilink (e.g. [[bee#English]])",
					"template": "Using {{section link}} (e.g. {{section link|bee#English}})"
				}
			}
		}
	}
);
const linkType = pref.linkType;

const linkIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="currentColor"><g><path d="M4.83 15h2.91a4.9 4.9 0 01-1.55-2H5a3 3 0 110-6h3a3 3 0 012.82 4h2.1a5 5 0 00.08-.83v-.34A4.83 4.83 0 008.17 5H4.83A4.83 4.83 0 000 9.83v.34A4.83 4.83 0 004.83 15"></path><path d="M15.17 5h-2.91a4.9 4.9 0 011.55 2H15a3 3 0 110 6h-3a3 3 0 01-2.82-4h-2.1a5 5 0 00-.08.83v.34A4.83 4.83 0 0011.83 15h3.34A4.83 4.83 0 0020 10.17v-.34A4.83 4.83 0 0015.17 5"></path></g></svg>`;

mw.util.addCSS(`
	.wikt-header-anchor {
		color: var(--wikt-palette-darkgrey) !important;
	}
	body:not(.skin-minerva) .mw-heading {
		position: relative;
	}
	body:not(.skin-minerva) .wikt-header-anchor {
		position: absolute;
		right: 0;
		opacity: 0;
		transition: opacity 0.15s ease;
	}
	body:not(.skin-minerva) .mw-heading:hover > .wikt-header-anchor {
		opacity: 1;
	}
	body.skin-minerva .wikt-header-anchor > svg {
		vertical-align: middle;
	}
`);

const elem = s => document.createRange().createContextualFragment(s).firstChild; // helper

for (let header of document.querySelectorAll(".mw-heading:not(.ext-discussiontools-init-section) > :is(h1, h2, h3, h4, h5, h6)")) {
	let linkElem = elem(`<a class="wikt-header-anchor" role="button">${linkIcon}</a>`);
	linkElem.addEventListener("click", event => {
		event.preventDefault();

		let copied = `https://en.wiktionary.org/wiki/${mw.config.values.wgPageName}#${header.id}`; // fullUrl
		if (linkType === "wikilink") {
			copied = `[[${mw.config.values.wgPageName}#${header.id}]]`.replaceAll("_", " ");
		} else if (linkType === "template") {
			copied = `{{section link|${mw.config.values.wgPageName}#${header.id}}}`.replaceAll("_", " ");
		}

		navigator.clipboard.writeText(copied)
			.then(() => mw.notify("Section link copied to clipboard."))
			.catch(err => mw.notify("An error occurred while copying section link:\n", err.stack));
	});

	if (document.body.matches(".skin-minerva")) {
		// add classes to make it look the same as the edit button
		linkElem.classList.add("cdx-button", "cdx-button--size-large", "cdx-button--fake-button", "cdx-button--fake-button--enabled", "cdx-button--icon-only", "cdx-button--weight-quiet");
		header.parentElement.querySelector(".mw-editsection").prepend(linkElem);
	} else {
		header.after(linkElem);
	}
}
// </nowiki>