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])*301.0);}
var size = parseInt((res[0]/res[1]) * 301.0);
var filename = document.getElementById("firstHeading").textContent;
var credits = document.getElementById("fileinfotpl_aut").nextSibling.nextSibling.textContent.replaceAll("\n","")
var license = document.getElementsByClassName("licensetpl_short")[0].innerHTML;
if (license == "Public domain") {license = "PD";}
var scaling = String(size);
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
} );