YUI anywhere (bookmarklet)
Hooked on YUI? You can now take it anywhere you go. The thing is Yahoo hosts the libraries publicly, so they are available at any time. Let's say you visit a page and you want to do something with it. Comes the YUI bookmaklet that adds a new script tag to the page pointing to utilities.js that contains all YUI utilities (DOM, Event, DragDrop, Animation, Connection...). Then just open your Firebug console and start messing with the page. The powerful toolset that YUI is, is at your disposal.
The code
The code for the bookmarklet is really simple, just a question of adding a new script tag.
(function(){ var s = document.createElement('script'); s.src='http://yui.yahooapis.com/2.2.2/build/utilities/utilities.js'; document.getElementsByTagName('head')[0].appendChild(s); })()
Install
Right-click, add to favorites, or drag to bookmarks.
Let the fun begin
So you go to any page, click the bookmarklet and for example decide to make the logo on the page draggable. All you need to write in your JS console (or in address bar for IE) is:
new YAHOO.util.DD('logo')
The result is really not bad for a one-liner. But why stop there? Let's make everything on a page draggable.
var all = document.getElementsByTagName('*');for(var i = 0; i < all.length; i++) {new YAHOO.util.DD(all[i])}
Whoa! We can mess up with other people's pages like we've never messed up with other people's pages before! Example:
And why only other people's pages, what about ours? Imagine you're sitting with a client or boss, showing the new site and they go:
- Hmm, well, you know, I don't know about this spacing between the images here ...
And you:
- Hold that thought (clicking bookmarklet, making everything draggable, dragging the offending image). Is this how you prefer it?
Client:
- Well, maybe, or actually it was better before.
You:
- Ah, OK, whatever suits your business needs
Update: Some more one-liners to prevent loading a new page when you attempt to drag a link or a submit button.
Disable links:
YAHOO.util.Event.addListener(window, 'click', function(e){ if (e.target.nodeName.toUpperCase() == 'A'){ YAHOO.util.Event.preventDefault(e) } });
Prompt before unload:
YAHOO.util.Event.addListener(window,'beforeunload',function(e){ e.returnValue = "Sure?" });

June 8th, 2007 at 2:55 am
The firebug console is one of my fav features. Being able to quickly inject code into a page, call a function, etc has made things so much easier for me.
I know there is other ways of doing it:
a) bookmarklet, but sometimes things are just too long…and typing all on online in the address bar can be a pain.
b) greasemonkey, but what if I only want to test some code once, quickly. No real point in writing a greasemonkey script!
I do have to admit I am a bit more of a prototype fan but am sure you can so similar in YUI is this one example which I use when there is a large amount of input type=’checkbox’ but no tick all….
$$(’input[type=checkbox]‘).each(function(el) {
el.checked=true;
});
but if you didn’t have the library attached that simple code gets alot longer
June 8th, 2007 at 10:54 am
Great bookmarklet! You should package this as a Greasemonkey script for Firefox. Or maybe somebody already has. I haven’t taken the time to check.
June 8th, 2007 at 4:32 pm
This is cool. Great job!
June 12th, 2007 at 11:17 am
[...] Intrepid soul, Stoyan Stefanov, recently figured out a way to have those same functions on any webpage you visit. Yeah, it works as part of Firefox with Firebug installed (or any browser with a full javascript console), but as he notes, it’s pretty handy for rejiggering and playing with pages without doing a lot of code. [...]