User:Enterprisey/copy-section-link.js
// Fixed version of User:Enterprisey/copy-section-link.js
//
$.when(
$.ready,
mw.loader.using( [ "mediawiki.util", "oojs-ui-core", "oojs-ui-widgets" ] )
).then( function () {
/*
* The convoluted function is needed, because there are different HTML
* layouts for "headings" in different skins.
* In Vector 2022, layout of ==Second level== versus ===Third level===
* headings is different even for a _single_ skin.
*
* The HTML layout is either
*
* or
*
*
* For details, see:
* - https://www.mediawiki.org/w/index.php?title=Heading_HTML_changes&oldid=6538029
*/
// Returns a plain HTMLElement
function findEditSectionForHeader(header) {
// in Vector, the bracketed [edit] section link is a direct child element/node
const maybeVectorEditSection = header.querySelector('.mw-editsection');
if (maybeVectorEditSection) {
return maybeVectorEditSection;
}
// in other skins, the bracketed [edit] section link is a sibling of
etc
if (header.parentElement.classList.contains('mw-heading')) {
const maybeEditSection = header.parentElement.querySelector('.mw-editsection');
if (maybeEditSection) {
return maybeEditSection;
}
}
return null;
}
/*
* The search for all section headings starts with
* finding all
* From the
*/
const allHeaders = $("#mw-content-text .mw-parser-output").find(":header").filter(':not(#mw-toc-heading)');
allHeaders.each( function(i, header) {
var popup = null;
var editSection = findEditSectionForHeader(header);
let target;
if (editSection === null) {
target = $(header);
console.warn('[copy-section-link]', 'edit section link not found:', target);
} else {
target = $(editSection);
}
target.after($( "", { "class": "copy-section-link-pilcrow" } )
.text( "¶" )
.click( function () {
if( popup === null ) {
const hash = header.id ? header.id : header.querySelector('.mw-headline')?.id;
var oldid = mw.util.getParamValue( "oldid" );
var popupContent;
function makeContent( pageName, id ) {
var wikitext = (pageName + "#" + hash).replace( /_/g, " " );
if( !window.copySectionLinkNoSquareBrackets ) {
wikitext = '' + wikitext + '';
}
return $( '
', { "class": "copy-section-link-content" } ).append(
$( "", { "id": "copy-section-wikilink" + id } ).text( wikitext ),
$( "