How to make an interactive bookmarklet part 1
Here’s how to put together a non-trivial interactive bookmarklet. For some reason there are not too many resources on the subject, but it has become a much more popular way of user’s interacting with a service without having to be on the site. Pinterest, Evernote, etc. all rely heavily on the ease of use of bookmarklets.
For Edorati, I needed to create a simple, fast way for user’s to publish an article to their online newspaper whenever they came across something they liked. It also had to look nice and allow them to select a picture thumbnail and add an editor’s note. Oh yeah it also needs to be upgradeable from the server side so that the user doesn’t have to reinstall it every time you want to add something new.
Why is it so hard?
Bookmarklets are especially hard for a couple reasons:
- You don’t own the page the bookmarklet will deploy on
- Cross-domain requests
- Maintaining some level of security
- Everything needs to be manipulated through javascript
- Avoid using full js libraries like jquery
What’s the plan?
I ended up building a simple framework for bookmarklets and that uses django to process the request, handle errors, and provide responses. The bookmarklet is actually just a small piece of javascript code that gets executed as soon as it is clicked. It will tell my server that it’s ready to be initialized for a particular user. Then the server will make a response with ONLY javascript that is contained in a self executing function which will manipulate the target page the user is on. From there any interactions are a matter of building a request on the client side or returning a javascript response from the server-side. Sound simple? Not really…
How do I make cross-browser requests, do I use Jquery?
No Jquery! We’re going to avoid all libraries and roll our own. Adding big libraries will make your bookmarklet slow and you probably only need a couple of things from it. All we need in this case is a small workaround to make cross-browser requests right from the user’s page. This is the pattern:
javascript:(function(){
var d=document,
// Create a script element
s=d.createElement("scr"+"ipt"),
// Set the src to an endpoint on your server
s.src = "http://www.urltoyourapi.com/?param1=something¶m2=somethingelse";
})()
Now your server can respond with javascript to fill in that script element that was just created on the user’s page. By adding some GET parameters to the end of the URL you also have a way of communicating information about the user or the page. For example you may want to construct the url in a way that it passes the users obfuscated ID number so you can save information to that user’s account. This is all a bookmarklet is.
Making it future proof
To prevent your users from having to install the bookmarklet every time you want to make a change, you should not include any presentation or interactive logic in the bookmarklet. For Edorati, all I need to know from the bookmarklet is who is the user. All the bookmarklet needs to do is essentially tell the server that it wants to be initialized. Everything else should happen on the server side. That way you can make any changes you want to the look or the functionality without ever having to change the user’s bookmarklet that they installed.
Next post will discuss constructing templated javascript on the server side to make a bookmarklet interactive, clean, and safe…
3 Notes/ Hide
-
ericjang likes this
-
e-mechanism likes this
-
chrishutchinson likes this
-
alexkehayias posted this
