FIxing text/plain HTML in Firefox

Firefox, correctly but annoyingly, displays HTML pages served as text/plain as the raw HTML rather than as rendered HTML. Someone has come up with a very useful bookmarklet called Force HTML which fixes this. It’s pretty clever; it just reads the text from the page into a string and then document.write()s it back into the page. I would not have thought of this!
I’d like to make this a GreaseMonkey script, so that I’m never bothered by this problem again, but there are two small issues with that. Issue 1 is that I can’t work out how to discover what the content-type a page was served as from JavaScript. (This isn’t a major issue; I could check for the document element being pre and the page content containing lt and gt characters and guess from that that it’s HTML being served as text/plain, probably.) Issue 2, a bit more seriously, is that when I do implement it as a GreaseMonkey script it crashes the browser. I am guessing that this is because GM runs before proper page load, and replaces the content of the page with a totally different page using document.write(). This probably screws things up. Once I’ve narrowed this down to a test case I’ll submit it as a GM bug, although I suspect Jeremy and Aaron and crew will say: don’t use document.write() to make the whole page disappear when you’re still in the middle of rendering that page.

14 Responses to “FIxing text/plain HTML in Firefox”

  1. Would attaching yourself as an onload handler work? The otherwise annoying flicker as it fully renders as text and then re-renders as HTML might serve as a handy warning, to remind you of what’s happening when it inevitably converts a plain text source example you didn’t want converted.

    Phil Ringnalda
  2. Phil: you’re too clever for words. I didn’t think of that. I’ll have a crack at that one, I think.

    sil
  3. For issue one, you could HEAD the host with XMLHttpRequest to get the content-type. Not a lot of code involved with that as evidenced with the rewrite of my response header viewer favelet.

    Steve
  4. Stuart,
    You are indeed correct that the cause is where Greasemonkey hooks in.
    We’re using DOMContentLoaded, an unpublisized Moz-specific event which has the benefit of occuring before rendering but after the DOM is loaded.
    This is the first mention of trouble we’ve had, and I’d generally say that avoiding the flicker problems associated w/ hooking in later are more valuable than allowing replacement of the DOM.

    Phil’s solution is indeed a good approach.

    Good data, but I don’t think this qualifies as a GM bug, do you?

    Jeremy Dunck
  5. Jeremy: I agree that it’s not a GM bug, which is why I haven’t filed one :)
    I shall just addEventListener() an onload handler instead, as Phil suggested. No idea why I didn’t think of this myself; must have been being thick that day.

    sil
  6. what’s the status of this project?

    Bill Donnelly
  7. Bill: it’s in “I’ll do it when I get a sufficiently round tuit” state. Meanwhile, the bookmarklet referenced above does do the work required.

    sil
  8. The bookmarklet above doesn’t work for me
    (in the one case I tried it on;
    and I don’t like the code, anyway),
    and I didn’t want to wait for a solution,
    so I decided to mess with it myself.
     
    Here is my rather elegant solution
    (if I do say so myself; and I often do)
    as a bookmarklet that I will make into a GmScript.
    Whether I publish it, or not, will depend on
    what others come up with, if anything.
     
    SPOILER ALERT! Don’t look at the following code
    if you want to solve the problem yourself.
     
    javascript:(function()
    {var sDoc=window.document.body.innerHTML;
    if(sDoc.substr(0,11)==”

    <span>"&&
    sDoc.search(/&lt;html&gt;/i)!=-1&&
    sDoc.search(/&lt;\/html&gt;/i)!=-1){
    sDoc=sDoc.substr(11).substr(0,sDoc.length-24);
    window.document.open();
    window.document.writeln(sDoc.replace(/&([gl])t;/g,
    function(pStr,pP1,pOffset,pS){
    return(pP1=='g'?'>':'<')}));
    window.document.close();}})();</tt></big>
    &nbsp;
    (I had to mess with the code to make it
    display correctly here -- hope it worked)

    Bill Donnelly
  9. hey there
    great to see someone is having the same problem as me, i tried the bookmarklet – added it to my bookmarks, but when i am on a page and click it it just brings up a blank page – any other ideas on how to fix this.
    i take it the code above works, however i am not 100% sure how to use it but some pointers there could sort that
    any help would be much appreciated
    thanks
    fergus

    fergus
  10. hey there
    great to see someone is having the same problem as me, i tried the bookmarklet – added it to my bookmarks, but when i am on a page and click it it just brings up a blank page – any other ideas on how to fix this.
    i take it the code above works, however i am not 100% sure how to use it but some pointers there could sort that
    any help would be much appreciated
    thanks
    fergus

    fergus
  11. ps, sorry bout triple post – doesn’t seem to be anything here to remove it

    fergus
  12. Hi,

    On my version of Firefox, there is only < PRE > instead of < PRE > < SPAN > so the

    sDoc=sDoc.substr(11).substr(0,sDoc.length-24)

    becomes:

    sDoc=sDoc.substr(5).substr(0,sDoc.length-11)

    I last tested this on Firefox 1.0.2, I dunno about 1.0.3.

    Eric Darchis
  13. does this not work with safari or…?

    Anonymous
  14. Anonymous: no idea, I’m afraid.

    sil

Leave a Reply

OpenID is a decentralised authentication system. If you use LiveJournal or Vox you already have an OpenID; just use the URL of your homepage there. See also how to get yourself an OpenID.