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.
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.
9 hours later
Phil: you’re too clever for words. I didn’t think of that. I’ll have a crack at that one, I think.
10 hours later
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.
12 hours later
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?
36 hours later
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.
37 hours later
what’s the status of this project?
3 days later
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.
3 days later
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(/<html>/i)!=-1&& sDoc.search(/<\/html>/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> (I had to mess with the code to make it display correctly here -- hope it worked)6 days later
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
9 days later
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
9 days later
ps, sorry bout triple post – doesn’t seem to be anything here to remove it
9 days later
Hi,
On my version of Firefox, there is only < PRE > instead of < PRE > < SPAN > so the
becomes:
I last tested this on Firefox 1.0.2, I dunno about 1.0.3.
4 weeks later
does this not work with safari or…?
56 weeks later
Anonymous: no idea, I’m afraid.
56 weeks later