2

I'm writing some user-JS for Opera. It reacts on a request that doesn't have an extension, e.g. /stuff/code/MyFile, or has one not related to JavaScript, e.g. /stuff/code/load.do. The content-type of the response is set to text/html, even though it returns pure JavaScript source (text/javascript). As I don't have access to the server code I simply have to live with this.

The problem now is that I want to format the source with line numbers and such and display it inside Opera. Therefore, I wrote some user-JS to react on AfterEvent.DOMContentLoaded (also tried AfterEvent.load, same thing). It reads e.event.target.body.innerHTML to gain access to the body, i.e. the JavaScript-code.

That alone would work nicely, if only the source wouldn't contain HTML-tags or comparison operators (<, >). Since it does, I never get the output I want. Opera seems to have some internal logic to convert the text/html-response into its own representation format. This includes that e.g. a CRLF after a HTML-tag is removed or code between two "matching" < and > (comparison operators!) are crunched together into one single line applying ="" after each word in there.

And that's where the problem is.

If I request the same URL without my user-JS and then look at the source of the "page" I see a clean JavaScript-code identical to what the server sent out. And this is what I want to get access to.

If I use innerText instead of innerHTML, Opera strips out the HTML-tags making the file different to the original, too.

I also tried to look at outerHTML, outerText and textContent, but they all have the same problems.

I know that Opera doesn't do anything wrong here. The server says it's a text/html and Opera simply does what it usually does with a text/html-kind of response.

Therefore, my question is: is there any way to get the untouched response with a user-JS?

sjngm
  • 12,423
  • 14
  • 84
  • 114
  • when Opera got text/javascript (or other text/* but not text/html) response, it still encloses in `
    actual text goes here
    `, and there is no way to determine if response was text/javascript.
    – Free Consulting Feb 16 '11 at 13:41

1 Answers1

0

There isn't any way to access the pre-parsed markup from JS. The only way to do that would be to use XMLHttpRequest to request the content yourself.

hallvors
  • 6,069
  • 1
  • 25
  • 43
  • For the protocol: I choose this as the right answer, but it doesn't solve my problem. – sjngm Dec 28 '10 at 00:45
  • use with caution, XHR to location.href is very fragile, Opera has problems with caching of current document (eg: if HEAD via XHR sent too early Opera will not display document at all, or it can present you everlasting "request to ... complete") – Free Consulting Feb 16 '11 at 13:39