User:NasssaNser/DiscussionLink.js

/* * Copyright (c) 2022-2023 NasssaNser. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, *   this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, *   this list of conditions and the following disclaimer in the documentation *   and/or other materials provided with the distribution. * 3. Neither the name of NasssaNser nor the names of its contributors may be *   used to endorse or promote products derived from this software without *   specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */

/** * For each section, adds a link to itself. */ "use strict";

$(function {	function createSectionLink(section, text) {		text = text || {			link: "link",			linkTitle: "Link to this section",			copyLink: "copy link",			copyLinkTitle: "Copy a wikilink to this section",		};

var id = $(section).find(".mw-headline").first.attr("id"); var link = $("") .attr("href", "#" + id) .attr("title", text.linkTitle) .text(text.link); // Copy wikilink on click if ("clipboard" in navigator) { link.on("click", function(event) {				event.preventDefault;				var section = mw.config.get("wgPageName") + "#" + id;				var wikilink = new Blob(["" + section + ""], { type: "text/plain" });				navigator.clipboard.write([new ClipboardItem({					"text/plain": wikilink				})]);			}); link.text(text.copyLink) .attr("title", text.copyLinkTitle); }		return link[0]; }

function addSectionLinkAfterHeader(header) { var link = createSectionLink(header); $(header).find(".mw-editsection") .after($(" ")				.addClass("us-sectionlink")				.addClass("mw-editsection-like")				.append($(" ") .append(new Text("[")) .addClass("mw-editsection-bracket") .addClass("us-sectionlink-bracket"))				.append(link)				.append($(" ") .append(new Text("]")) .addClass("mw-editsection-bracket") .addClass("us-sectionlink-bracket"))); }

var container = $(".ext-discussiontools-visualenhancements-enabled"); if (container.length) container.find(".ext-discussiontools-init-section").each(function {			var link = createSectionLink(this, { link: "Link", linkTitle: "Link to this discussion", copyLink: "Copy link", copyLinkTitle: "Copy a wikilink to this discussion" });			$(this).find(".ext-discussiontools-init-section-metadata").first				.append($(" ") .addClass("ext-discussiontools-init-section-metaitem") .addClass("us-sectionlink") .addClass("us-sectionlink-discussionlink") .append(link));		}); else $("body").find("h2").each(function {			addSectionLinkAfterHeader(this);		}); $("body").find("h3, h4, h5, h6").each(function {		addSectionLinkAfterHeader(this);	}); });