Jump to content

User:PotsdamLamb/scripts/BareURLinline.js

From Simple English Wikipedia, the free encyclopedia
Revision as of 18:02, 29 July 2022 by PotsdamLamb (talk | changes) (Created page with "// /BareURLinline.js -- v3.1, with support for {{Bare URL PDF}} and {{Bare URL image}} // Install with: // <code><nowiki> {{subst:Iusc|PotsdamLamb/scripts/BareURLinline.js}} </nowiki></code> // or with // <code><nowiki> importScript( 'PotsdamLamb/scripts/BareURLinline.js' ); // Backlink: PotsdamLamb/scripts/BareURLinline.js </nowiki></code> // This script is hacked from User:DannyS712/Draft no cat.js // If forking this script, please note our...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// /BareURLinline.js -- v3.1, with support for {{Bare URL PDF}} and {{Bare URL image}}
// Install with:
// <code><nowiki>		{{subst:Iusc|PotsdamLamb/scripts/BareURLinline.js}}																</nowiki></code>
// or with
// <code><nowiki>		importScript( 'PotsdamLamb/scripts/BareURLinline.js' ); // Backlink: [[PotsdamLamb/scripts/BareURLinline.js]]			</nowiki></code> 

// This script is hacked from [[User:DannyS712/Draft no cat.js]]
// If forking this script, please note our contributions / give us credit
// This is a fork taken from [[:EN:User:BrownHairedGirl]] who hacked it from [[:EN:User:DannyS712]]
// This is for simple wikipedia only and will not work on another language. For english, us the main one I customized this // // from
if ( mw.config.get( 'wgNamespaceNumber' ) === 0) {
    mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () { 
        var link = mw.util.addPortletLink( 'p-cactions', '#', 'BareURLinline', 'ca-bareurlinline', 'BareURLinline'); 
        $( link ).click( function ( event ) {
            event.preventDefault();
            tag_bareurlinline();
        } );
    } );
} );
}
function tag_bareurlinline(){
	var edit_summary = " tag(s) using [[User:PotsdamLamb/scripts/BareURLinline.js|a script]]. "
                   + "For other recently-tagged pages with [[WP:Bare URLs|bare URLs]], "
                   + "see [[:Category:Articles with bare URLs for citations from " + bui_monthyear_datestamp() + "]]";

	var send_req = {
	    action: 'query',
	    titles: mw.config.get( 'wgPageName' ),
	    prop: 'revisions',
	    intoken: 'edit',
	    rvprop: 'content',
	    indexpageids: 1,
	    dataType: 'xml',
	    format: 'xml'
	};
	var buiTagsAdded = 0;
	var buPDFtagsAdded = 0;
	var buimgtagsAdded = 0;
	var wgPageName = mw.config.get( 'wgPageName' );
	$.get( mw.config.get( 'wgScriptPath' ) + '/api.php', send_req, function( response ) {
	    var text = $( response ).find( 'rev' ).text();
		const PDFURLregex = /\.pdf$/i;
		const imageURLregex = /\.(JPG|JPEG|JP2|JPX|JPE|JFIF|JIF|TIFF|TIF|GIF|BMP|PNG|APNG|MNG|PBM|PGM|PPM|PNM|WEBP|HDR|HEIF|HEIFS|HEIC|HEICS|AVCI|AVCS|AVIF|AVIFS)$/i;
	    var newText = text
	    		.replace(/(?<begin><ref[^>]*?\>)\s*\[?\s*(?<url>https?:[^>< \|\[\]]+)\s*\]?\s*(?<end><\s*\/\s*ref)/gi,
	    			function(match, p1, p2, p3) {
						if (p2.match(PDFURLregex)) {
							buPDFtagsAdded += 1;
							return  p1 + p2 + ' {{Bare URL PDF|date=' + bui_monthyear_datestamp() + '}}' +p3;
						}
						else if (p2.match(imageURLregex)) {
							buimgtagsAdded += 1;
							return  p1 + p2 + ' {{Bare URL image|date=' + bui_monthyear_datestamp() + '}}' +p3;
						}
						else {
							buiTagsAdded += 1;
							return  p1 + p2 + ' {{Bare URL inline|date=' + bui_monthyear_datestamp() + '}}' +p3;
						}
	    			}
	    			);
	if (text == newText) {
		// nothing was changed
		alert("[[" + wgPageName + "]]: No untagged bare URLs");
		return;
	}
	newText = newText.replaceAll("<br>", "<br />");
	newText = newText.replaceAll("<br/>", "<br />");
	var tagsAdded = buiTagsAdded + buPDFtagsAdded + buimgtagsAdded;
	if (confirm("[[" + wgPageName + "]]: " + tagsAdded + " untagged bare URLs.\n\nTag them with {{Bare URL inline}}?")) {
		var summaryExtra = "";
		var mytagsummary = "";
		if (buiTagsAdded > 0) {
			mytagsummary = mytagsummary + buiTagsAdded + " {{[[Template:Bare URL inline|Bare URL inline]]}} ";
		} 
		if (buPDFtagsAdded > 0) {
			summaryExtra = summaryExtra + " and [[:Category:Articles with PDF format bare URLs for citations]]";
			if (mytagsummary != "") {
				mytagsummary = mytagsummary + "and ";
			}
			mytagsummary = mytagsummary + buPDFtagsAdded + " {{[[Template:Bare URL PDF|Bare URL PDF]]}} ";
		} 
		if (buimgtagsAdded > 0) {
			summaryExtra = summaryExtra + " and [[:Category:Articles with image file bare URLs for citations]]";
			if (mytagsummary != "") {
				mytagsummary = mytagsummary + "and ";
			}
			mytagsummary = mytagsummary + buimgtagsAdded + " {{[[Template:Bare URL image|Bare URL image]]}} ";
		} 
		var submit = {
					action: 'edit',
					pageid: mw.config.get( 'wgArticleId' ),
					text: newText,
					summary: "Added " + mytagsummary + " " + edit_summary + summaryExtra,
					minor: true,
					token: mw.user.tokens.get( 'csrfToken' )
				};
	    $.when(
	        $.post( mw.config.get( 'wgScriptPath' ) + '/api.php', submit, function( response ){ } )
	    ).done( function() { location.reload();} );
	}
	} );
}

function bui_monthyear_datestamp() {
var d = new Date();
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";

var myyear = d.getFullYear();

let mydatestamp = month[d.getMonth()] + " " + myyear.toString();
return mydatestamp;
}