The Data: URI kitchen utility (hixie.ch)

By mungushume Last update Nov 16, 2007 — Installed 390 times. Daily Installs: 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0
// ==UserScript==
// @author         mungushume
// @version        1.0.2
// @name           The Data: URI kitchen utility (hixie.ch) 
// @namespace      mungushume_@_hotmail_._com
// @description    Makes hixie's data tool work on one page (using iframes). Gives you easily selectable data via a double click. Displays the data on one or multiple lines. Tells you the size of the string data.
// @include        http://software.hixie.ch/utilities/cgi/data/data
// @scriptsource   http://userscripts.org/scripts/show/9240
// ==/UserScript==

// Version 1.0.1 some minor cosmetic lifts as recommended by znerp
// Version 1.0.2 some more major cosmetic lifts as recommended and coded by alien_scum

(function(){

Number.prototype.rounddp=function(dp){return Math.round(this*Math.pow(10,dp))/Math.pow(10,dp);}

function ScaledSize(Value, DP){
    if (Value > 1024) {
        if (Value > Math.pow(1024, 3)){
            return (Value / Math.pow(1024, 3)).rounddp(DP) + "GB";
        }else if (Value > Math.pow(1024, 2)){
            return (Value / Math.pow(1024, 2)).rounddp(DP) + "MB";
        }else{
            return (Value / 1024).rounddp(DP) + "KB";
        }
    }else{
        return Value + "B";
    }
}

var a = document.evaluate ("/html/body/p/a[starts-with(@href,'data')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
var frm = document.evaluate ("/html/body/form", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

if(a){
	
	var warn = document.evaluate ("/html/body/p[1]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
	if (warn.innerHTML.indexOf("redirected") != -1){
		warn.parentNode.removeChild(warn);
	}

	var body = a.parentNode.parentNode;
	
	var p = document.createElement("p");
	p.innerHTML=" "+ScaledSize((a.href.length+2),2)+" as a single line. ("+ (a.href.length+2) +" chars)";
	p.setAttribute("style", "font-family:'Courier New', Courier, monospace; font-size:11px;");
	body.insertBefore(p, a.nextSibling);

	var p = document.createElement("p");
	var input = document.createElement("input");
	input.setAttribute("type", "text");
	input.setAttribute("size", "108");
	input.setAttribute("title", "Double click to select all");
	input.setAttribute("style", "font-family:'Courier New', Courier, monospace; font-size:11px; width: 100%; color:#000;");
	input.setAttribute("value", '"'+a.href+'"');
	input.setAttribute("onDblClick", "javascript:this.focus();this.select();");
	p.appendChild(input);
	body.insertBefore(p, a.nextSibling);

	var out=''
	for(var i=0; i<a.href.length; i+=100)
	{
		out+='"'+ a.href.substr(i,100) +'"';
		if((i+100)<a.href.length){out+='+';}
		out+='\n';
	}

	var p = document.createElement("p");
	p.innerHTML="&nbsp;"+ScaledSize((out.length),2)+" as multiple lines. ("+ out.length +" chars, "+ (i/100) +" lines)";
	p.setAttribute("style", "font-family:'Courier New', Courier, monospace; font-size:11px;");
	body.insertBefore(p, a.nextSibling);

	var p = document.createElement("p");
	var textarea = document.createElement("textarea");
	textarea.setAttribute("type", "text");
	textarea.setAttribute("cols", "105");
	textarea.setAttribute("rows", (i/100));
	textarea.setAttribute("title", "Double click to select all");
	textarea.setAttribute("style", "font-family:'Courier New', Courier, monospace; font-size:11px; width: 100%; color:#000;");
	textarea.setAttribute("wrap", "physical");
	textarea.setAttribute("onDblClick", "javascript:this.focus();this.select();");
	textarea.innerHTML=out;

	p.appendChild(textarea);
	body.insertBefore(p, a.nextSibling);
	parent.document.getElementById("resultsframe").style.height=this.document.body.scrollHeight+40+"px";
	
}else if(frm){

	var chk = document.evaluate ("//p/label/input[@name='base64']", frm, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
	if (chk) {chk.checked=true;}
	
	frm.setAttribute("target", "resultsframe");
	
	var submit=document.evaluate ("//p/input[@type='submit']", frm, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
	submit.setAttribute("onClick", "javascript:document.getElementById('resultsframe').style.display='';");
	
	var p = document.createElement("p");
	var ifra = document.createElement("iframe");
	ifra.setAttribute("name", "resultsframe");
	ifra.setAttribute("id", "resultsframe");
	ifra.setAttribute("frameborder", "no");
	ifra.setAttribute("scrolling", "no");
	ifra.setAttribute("width", "100%");
	ifra.setAttribute("height", "0");
	ifra.style.display="none";
	
	p.appendChild(ifra);
	frm.parentNode.insertBefore(p, frm.nextSibling);
	
}
})();