Jump to content

User:JPxG/signpost-filetools.js

From Wikipedia, the free encyclopedia
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])*300.0);}
    	var size = parseInt(Math.ceil((res[0]/res[1]) * 300.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 credit;
		var license;
		try { credits  = document.getElementById("fileinfotpl_aut").nextSibling.nextSibling.textContent;} catch(e) {credits = "?";}
		try { license  = document.getElementsByClassName("licensetpl_short")[0].innerHTML;              } catch(e) {license = "?";}
		var scaling  = String(size);
		
		// Clean various crud out of these scraped attributes
		filename = filename.replaceAll("[edit]", "");
		
		if(credits.indexOf("commons-creator-table") !== -1){
			// This being in the string means that instead of an author name, we scooped up a huge template
			// If this happens, we should just start over and scrape the field properly from said template.
			credits = document.getElementById("creator").textContent;
		}
		
		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
} );