Turn JavaScript into bookmark or bookmarklet?
To solve your specific problem I managed to get your JavaScript code to execute without any error in this version:
javascript:(function(){function text(){this.type="text"};function password(){this.type="password"};function addHandlers(){var e=false;var t=document.evaluate("//input[@type='password']",document,null,6,null);for(var n=t.snapshotLength-1,r;r=t.snapshotItem(n);n--){if(!e){r.addEventListener("mouseover",text,false);r.addEventListener("mouseout",password,false)}else{r.addEventListener("focus",text,false);r.addEventListener("blur",password,false)}}};addHandlers();alert("bookmarklet loaded");})();
To see if your code works, I added an alert("bookmarklet loaded");
at the end of the bookmarklet. So you can try and should - since I tested on Firefox 17- see as a result a messagebox appears. If your code works or does not work I am unable to say, but of course this was not the question.
There are some things you can keep in mind or make transformation of JavaScript code within <script></script>
tags easier to be adopted for a bookmarklet
- In normal JavaScript code a newline can separate two commands. Bookmarklets are in one line so substitute the linebreak with a semicolon ;.
An example here
Let's have an example. Let's have two sourcecode snippets with two instructions:
var anumber=1
var atext="hallo"
When put all in one line (= make the linebreak to whitespaces) you would first end up with this:
var anumber=1 var atext="hallo"
This is not valid JavaScript code anymore. If you want to combine two instructions in one line you need to separate/delimit the two instructions by using a semicolon like this:
var anumber=1; var atext="hallo"
- Wrap all you code in kind of this kind of closure:
javascript:(function(){ [...here your code...] })();
There are quite some websites out there to investigate the solution. Like here.
You were actually quite close. You just need to put it this way:
javascript:(function(){mycode})();
In your example, you can try this:
javascript:(function(){function text(){this.type="text"};function password(){this.type="password"};function addHandlers(){var e=false;var t=document.evaluate("//input[@type='password']",document,null,6,null);for(var n=t.snapshotLength-1,r;r=t.snapshotItem(n);n--){if(!e){r.addEventListener("mouseover",text,false);r.addEventListener("mouseout",password,false)}else{r.addEventListener("focus",text,false);r.addEventListener("blur",password,false)}}};addHandlers()})();
Please pay attention to semicolons when you minify your code, you were missing some of them.