// ==UserScript==
// @name Detect Content-Type
// @version 1.0
// @description It alerts when the server send the wrong Content-Type (plain/text) for archives
// @author ale5000
// @namespace http://ale5000.altervista.org/
// @download-page http://userjs.ale5000.altervista.org/
// ==/UserScript==
if( document.body && document.body.childNodes.length == 1 && document.body.firstChild.tagName == "PRE" && document.body.firstChild.firstChild )
{
var start = document.body.firstChild.firstChild.nodeValue;
start = escape(start.substring(0,7));
var contentType = "", extension = "";
if(start.indexOf("Rar%21%1A%07") == 0)
{
contentType = "application/x-rar-compressed";
extension = "rar";
}
else if(start.indexOf("PK%03%04") == 0)
{
contentType = "application/zip";
extension = "zip";
}
else if(start.indexOf("7z%BC%AF%27%1C") == 0)
{
contentType = "application/x-7z-compressed";
extension = "7z";
}
else if(start.indexOf("%1F%u2039%08") == 0)
{
contentType = "application/x-gzip";
extension = "tar.gz, tgz, gz, gzip";
}
else if(start.indexOf("BZh") == 0)
{
contentType = "application/bzip2";
extension = "tar.bz2, bz2";
}
else if(start.indexOf("%1F%uFFFD%uFFFD") == 0)
{
contentType = "application/x-compress";
extension = "tar.z, z";
}
// tar (application/x-tar) contains "ustar" at position 101h (257) but I cannot see it from Javascript
if(extension != "")
{
var a = document.createElement("a");
var div = document.createElement("div");
var div2 = document.createElement("div");
a.innerHTML = "Download this file";
a.href = location.href;
div.innerHTML = "Content-type: " + contentType + "
Extension: " + extension + "
Right click on the link and select \"Save object as...\"";
div.style = "color: green;";
document.body.insertBefore(div, document.body.firstChild);
document.body.insertBefore(a, div);
}
}