6

I have created one script that shows passwords on hover just like Windows 8 on eye click.

I have made this functionality through JavaScript with following code.

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()

Sorry for the packed JavaScript code, but I want to create a bookmark or bookmarklet that run thiss script...

I tried:

javascript:(mycode);

But it didn't work...

  • bookmark is not very specific. There is more than one program that knows something like a bookmark/bookmarklet. Of course it seems that you mean a browser. Well there is Firefox, IE, Opera etc.. which one? – humanityANDpeace Jan 8 '13 at 12:33
8

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.

  • In normal javascript code a newline can seperate two commands. Bookmarklets are in one line so substitute the linebreak with a semicolon ; what does it mean.. can u elaborate more?? – vishal Jan 8 '13 at 13:36
  • 2
    I recommend inserting semicolons always, although you can read more about it here. – Karan Jan 8 '13 at 18:21
5

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.

  • P.S. After posting my answer I tried your code, and I like it! Useful trick, I saved this bookmarklet – Kar.ma Oct 26 '18 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.