Opera User-JS: how do I get the raw server response? - javascript

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?

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.

Related

Setting charset for a specific div

Is it possible to assign a charset for at specific div? So that you can have more than one charset on a page.
I'm currently importing snippets of text to my site via JS, and some of this text requires the UTF-8 charset. To be sure that my text is shown right on every page it is included (Sometimes external sites), I force the metatag into all the sites.
Is it possible to apply this charset to only a specific div, span or something like that?
No it is not, and it also entirely unnecessary.
The <meta> element declaring a charset or, better, the equivalent HTTP header is only there to help the browser correctly interpret the HTML text. Once the browser has done so, it constructs a DOM out of it and you may essentially treat the text as having no concrete charset after this point. For all intends and purposes the text exists as text in the DOM, not as binary representation which must be interpreted by a charset decoder.
When you're adding new content to the DOM via Javascript, the same ideas apply. The browser needs to fetch the new content via HTTP and the content's encoding should be denoted by an HTTP header. The browser can convert the text from the specific encoding to "DOM text" based on that, after which is doesn't matter anymore what encoding it was in.
Therefore, you can perfectly mix and match encodings from different sources being delivered in separate HTTP responses within the same page/DOM without having to worry about a "global" encoding.
I think, You can't use multi charset on one page, but use an iframe can resolve it the problem.

How can I use JSON.parse in a bookmarklet in IE9 if the page is not in standards mode?

I'm creating a bookmarklet that will accept a JSON string through a prompt like so:
prompt("Enter your JSON", "");
Once they enter their JSON string, I convert their string into a JSON Object using JSON.parse. Unfortunately, as far as I can tell JSON.parse is not supported in IE9. The object of my bookmarklet is to loop through the available <input>'s on the page and fill their values with data from my JSON object.
What can I do in order to get this to work in IE9?
One possible solution - This answer: https://stackoverflow.com/a/7146404/556079 suggests that JSON.parse will work if my document is in standards mode. Since this is a bookmarklet, I have no control over the doctype of the page. Would it be possible to change the doctype using my bookmarklet?
I would prefer to avoid loading libraries in my bookmarklet like http://bestiejs.github.io/json3. If this can be done without resorting to that, that would be ideal.
Alternatively, the input doesn't need to even be a JSON. I just need to get some user submitted data into a loop where I can reference anywhere between 2 and 50 values.

How do I check in javascript if the html page was valid?

Is there a way to query the browser if the page that loaded the javascript was valid, at least as far as the browser is concerned? Obviously the browser loads this page and not so much as validates it, but rather interprets it for display. Is there a way to query the list of errors and warnings that the browser generated when processing the html?
This would be a neat way to generate warning in selenium for syntax of the page.
Out of sheer necessity browsers do not actually validate html in any way and only parse it.
If you wish to know if the browser had any issue parsing it, you can take a stringified version of the original html, and compare it to a stringified version of the HTML after the browser has parsed it.
If the browser encountered any parsing issues (no matter how small), it will have edited your HTML source in order to make the DOM tree generate properly.
Note though that even this method is not foolproof, because the browser will only fix problems it can understand, for example, using an invalid html tag has no effect as far as the browser is concerned when it comes to parsing your html.
You could AJAX in the W3C validator and interpret the results. Something like this:
jQuery(function($){
var yourURL = window.location.href;
var urlencode = encodeURIComponent(yourURL);
$('#your-results-container').load('http://validator.w3.org/check?uri='+urlencode+' #results');
});`
This would take your current URL, urlencode it, run it through the W3C validator and load the results div of that page into your div "your-results-container". You could then parse through it and do whatever you wanted with IDs or classes of errors or warnings.

Parsing very large JSON strings in IE causing problems

I'm parsing a 2MB JSON string in IE8. The JSON.Parse line is taking a little while to return and IE8 shows a message asking the user if they want to abort the script.
Is there any way I can suppress this message? (or somehow speed up JSON.Parse)
I know about Microsoft KB175500, however this is not suitable as my target users will not have administrator access to make the registry modifications on their SOE machines.
I had this same question. Apparently there is no way to suppress the message, but there are tricks to make IE think it's still working by using an asynchronous iteration pattern (dead link, view comments below).
This comes from an answer to one of my questions:
loop is too slow for IE7/8
If the browser is unhappy with how long the JSON parser is taking, there are only four choices here I know of:
Get a faster JSON parser that doesn't take so long.
Break up your JSON data into smaller pieces so you are only parsing smaller pieces at once.
Modify a JSON parser to work in chunks so it can parse part of the data in one chunk, then on a short timeout, parse the next chunk, etc... This will prevent the browser prompt, but is probably a lot of work to write/modify a JSON parser that works this way.
If you can be sure the content is safe, then you could see if using eval instead of a JSON parser works around the issue.

Want to create a multilingual javascript object, how to handle if lots of labels?

I want to create a javascript file for multilingual functionality
i.e. display the error message in the correct language.
If I have allot of labels for a page, error messages, etc., what is a smart way of making this so the actual output on the page isn't huge?
i guess the best way is to somehow output the labels that I need ONLY?
lang.getkey('username');
will output the correct label, depending on the language.
Language detection is usually done server-side by checking the Accept-Language HTTP header that is sent.
Browsers have limited, non-standardized means for identifying a user's language (with the exception of IE running on Windows). With IE on Windows, you can access navigator.userLanguage or navigator.systemLanguage, which will return the operating system's RFC #4646 language-COUNTRY code. Other browsers (Opera, Safari, Chrome, Firefox) provide navigator.language, which is in the same format with the exception of Opera which returns the language only. In many cases this might be good enough, but it's still recommended to use a server solution.
I achieved something like this a while ago by separating the strings into different lang.js files and added the script to the document using document.write(). The function would simply fetch the string from an array defined in that lang.js file. A basic example might be:
// Get the language-COUNTRY code, and strip it to the language part only
var lang = (navigator.language || navigator.userLanguage).substring(0,2);
var file = "lang/" + lang + ".js";
document.write('<script src="'+lang+'" type="text/javascript"><\/script>');
This would ensure that only the strings for the necessary language were loaded, although I haven't included a fallback method here, you would need one for defaulting to a language when an unsupported one was detected. You could do this by having a list of supported languages in an array, check to see if lang exists and if it doesn't, write a default script src instead.
Not to sound like a broken record, but you should probably be determining languages and including files server side, not client side.

Categories

Resources