[Greasemonkey] Getting Content-Type of a document ??

esquifit esquifit at yahoo.de
Tue Jan 17 12:01:24 EST 2006


On Mon, Jan 16, 2006 at 11:22:54PM -0800, Charles Iliya Krempeaux wrote:
> Thanks!
> 
> On 1/16/06, Lenny Domnitser <ldrhcp at gmail.com> wrote:
> >
> > document.contentType
> >
>
This gives you the content type of the current document, of course.  If
you are interested in knowing the content type of those documens linked
to in the current page, I think the best thing is to make an http HEAD
request for the link, and then to parse the Content-Type header,
something along the lines:

GM_xmlhttpRequest( { 
   method: 'HEAD',
   url: your_url, //  <-------- here goes the url you're interested in
   headers: { 'User-agent': 'Firefox/1.5' },
   onload: function(responseDetails) {
             if ( responseDetails.status == '200') {
                var headersArray = responseDetails.responseHeaders.toString().split("\n");
                var contentType = getContentType(headersArray));
                //
                // do whatever you want with contentType
                // ....
             }
           }
} );

function getContentType(headers) {
   var pairs = {};
   for (h in headers) {
     aPair = headers[h].split(":");
     pairs[aPair[0]]=aPair[1];
   }
   var contentType = null;
   if ( pairs["Content-Type"]) 
     // strip leading space
     contentType = pairs["Content-Type"].match(/\S.*/); 
   return(contentType);
}

Probably you'll have to convert all headers(only the part before the first ":"!) to lowercase before matching
against a string like "content-type", I'm not 100% sure but I think http
headers are not case sensitive.


More information about the Greasemonkey mailing list