Jump to content

User:JPxG/signpost-filetools.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by JPxG (talk | contribs) at 03:52, 10 June 2024 (some more fixes). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$( function() {
    if( (window.location.href.indexOf( "File:" ) >= 0 ) || (window.location.href.indexOf( "File%3A" ) >= 0 )) {
    	var debug = 1;
    	if(debug == 1) {console.log("starting script");}
    	var res = document.getElementsByClassName("fileInfo")[0].innerHTML.replaceAll(/ pixels.*/g, "").replaceAll("(","").replaceAll("SVG file, nominally","").replaceAll(" ","").split("×");
    	// should produce [width, height].
    	if(debug == 1) {console.log("width/height are");}
    	if(debug == 1) {console.log(res);}
    	res[0] = parseFloat(res[0].replaceAll(",",""));
    	res[1] = parseFloat(res[1].replaceAll(",",""));
    	if(debug == 1) {console.log("parseFloated, those are:");}
    	if(debug == 1) {console.log(res);}
    	if(debug == 1) {console.log("0/1 is:");}
    	if(debug == 1) {console.log(res[0]/res[1]);}
    	if(debug == 1) {console.log("* 301 is:");}
    	if(debug == 1) {console.log((res[0]/res[1])*301.0);}
    	var size = parseInt((res[0]/res[1]) * 301.0);
    	if(size < 300){ size = 300;} // If it's portrait orientation, a width below 300 will letterbox it the OTHER way!
		
		var filename = document.getElementById("firstHeading").textContent;
		var credits  = document.getElementById("fileinfotpl_aut").nextSibling.nextSibling.textContent
		var license  = document.getElementsByClassName("licensetpl_short")[0].innerHTML;
		var scaling  = String(size);
		
		// Clean various crud out of these scraped attributes
		filename = filename.replaceAll("[edit]", "");
		
		credits  =  credits.replaceAll("\n","");
		credits  =  credits.replaceAll("(talk)", "");
		credits  =  credits.replaceAll("(Uploads)", "");
		credits  =  credits.replaceAll("Unknown authorUnknown author", "Unknown author");
		if(credits == "Unknown author") {credits = "anon.";}
		if(credits == "unknown author") {credits = "anon.";}
		if(credits == "Anonymous")      {credits = "anon.";}
		if(credits == "anonymous")      {credits = "anon.";}
		
		license  =  license.replaceAll("Public domain", "PD");
		license  =  license.replaceAll("No restrictions", "PD");
		
	
		var template = ""
		template +=   " |piccyfilename      = " + filename;
		template += "\n |piccy-credits      = " + credits;
		template += "\n |piccy-license      = " + license;
		template += "\n |piccy-xoffset      = " + "0";
		template += "\n |piccy-yoffset      = " + "0";
		template += "\n |piccy-scaling      = " + scaling;
		template += "\n}}";
	
    	// Put this in a details-summary block so you can get to it from the file page.
    	
        var details = document.createElement('details');
        var summary = document.createElement('summary');

        summary.textContent = "Use in Signpost template";
        // This works on Wikipedia but not Commons -- for Commons you have to use a pre
        // var syntaxhighlight = document.createElement('syntaxhighlight');
        // syntaxhighlight.setAttribute("lang", "wikitext");
        var syntaxhighlight = document.createElement('pre');
        syntaxhighlight.textContent = template;

        details.appendChild(summary);
        details.appendChild(syntaxhighlight);

    	details.class = "signpost-filetools";
    	
    	if(debug == 1) {console.log("appending to resolutioninfo");}
    	document.getElementsByClassName("mw-filepage-resolutioninfo")[0].appendChild(details);
    } // if page title has "File:" in it
} );