User:JPxG/signpost-filetools.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:JPxG/signpost-filetools. |
$( 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
} );