User:Novem Linguae/Scripts/TemplateTaxonomyAddCite.js

//

/* Adds some buttons (Cite iNaturalist, Cite WoRMS, Cite NCBI, Cite Other) to the edit screen of Template:Taxonomy subpages, and pre-fills it assuming it is a genus. Helpful for quickly creating Template:Taxonomy subpages, which is useful when getting to work with new species.

// TODO: fix bug where button doesn't work sometimes when clicked // TODO: if no talk page, create talk page and add correct WikiProject templates: amphibians and reptiles, animals, arthropods, beetles, birds, diptera, fishes, fungi, gastropods, insects, lepidoptera, mammals, marine life, microbiology, paleontology, plants

class TemplateTaxonomyAddCite { constructor( mw, $ ) { this.mw = mw; this.$ = $; }

async addLinksAndListeners { const title = this.mw.config.get( 'wgPageName' ); if ( !title.startsWith( 'Template:Taxonomy/' ) ) { return; }

let taxon = title.match( /(?<=\/).*$/ ); taxon = taxon[ 0 ];

// TODO: refactor to use JSON, and a loop to add the table rows

// last to first let html = `

#TemplateTaxonomyAddCite { margin: 1em 0; }

#TemplateTaxonomyAddCite, #TemplateTaxonomyAddCite th, #TemplateTaxonomyAddCite td { border: 1px solid black; border-collapse: collapse; padding: 0 5px; background-color: #ccffff; }

';		this.$( '.editpage-head-copywarn' ).after( html );

// Listeners have to be added after the elements are placed on the page for ( const website of websites ) { this.$( '#' + website.id ).on( 'click', function {				this.addCite( website.id );			}.bind( this ) ); }	}

addCite( websiteId ) { // CC BY-SA 4.0, Mitch3091, https://stackoverflow.com/a/38148759/3480193 const date = new Date.toISOString.slice( 0, 10 ); // yyyy-mm-dd

const title = this.mw.config.get( 'wgPageName' ); let taxon = title.match( /(?<=\/).*$/ ); taxon = taxon[ 0 ];

let wikicode = "\n';

this.$( '#wpTextbox1' ).html( wikicode );

// watchlist it		this.$( '#wpWatchthis' ).prop( 'checked', true );

const isCreatingPage = this.$( '#firstHeading' ).html.startsWith( 'Creating' ); if ( isCreatingPage ) { this.$( '#wpSummary' ).val( 'create (TemplateTaxonomyAddCite)' ); } else { // editing this.$( '#wpSummary' ).val( 'add/edit citation (TemplateTaxonomyAddCite)' ); }	} }

$( async function {	await ( new TemplateTaxonomyAddCite( mw, $ ) ).addLinksAndListeners; } );

//