Hack 20. Follow Links Without Clicking Them

 < Day Day Up > 

Hover over any link for a short time to open it in a new tab in the background.

This hack was inspired by dontclick.it (http://dontclick.it), a site that demonstrates some user interaction techniques that don't involve clicking. The site is written in Flash, which annoys me, but it gave me the idea of lazy clicking: the ability to open links just by moving the cursor over the link and leaving it there for a short time. I don't claim that it will cure your carpal tunnel syndrome, but it has changed the way I browse the Web.

2.9.1. The Code

This user script runs on nonsecure web pages. By default, it will not run on secure web pages, because it has been my experience that most secure sites, such as online banking sites, are very unweblike and don't support opening links in new tabs.

The script gets a list of all the links (which Firefox helpfully maintains for us in the document.links collection) and attaches three event handlers to each link:


Mouseover

When you move your cursor to a link, the script starts a timer that lasts for 1.5 seconds (1500 milliseconds). When the timer runs down, it calls GM_openInTab to open the link in a new tab.


Mouseout

If you move your cursor off a link within 1.5 seconds, the onmouseout event handler cancels the timer, so the link will not open.


Click

If you actually click a link within 1.5 seconds, the onclick event handler cancels the timer and removes all three event handlers. (Note that you can click a link without leaving the page; for example, holding down the Ctrl key while clicking will open the link in a new tab.) This means that if you manually follow a link, the auto-open behavior disappears and the link will not open twice.

Save the following user script as autoclick.user.js:

 // ==UserScript== // @name AutoClick // @namespace http://diveintomark.org/projects/greasemonkey/ // @description hover over links for 1.5 seconds to open in a new tab // @include http://* // ==/UserScript== var _clickTarget = null; var _autoClickTimeoutID = null; function mouseover(event) { _clickTarget = event.currentTarget; _autoclickTimeoutID = window.setTimeout(autoclick, 1500); } function mouseout(event) { _clickTarget = null; if (_autoclickTimeoutID) { window.clearTimeout(_autoclickTimeoutID); } } function clear(elmLink) { if (!elmLink) { return; } elmLink.removeEventListener('mouseover', mouseover, true); elmLink.removeEventListener('mouseout', mouseout, true); elmLink.removeEventListener('click', click, true); } function click(event) { var elmLink = event.currentTarget; if (!elmLink) { return false; } clear(elmLink); mouseout(event); } function autoclick( ) { if (!_clickTarget) { return; } GM_openInTab(_clickTarget.href); clear(_clickTarget); } for (var i = document.links.length - 1; i >= 0; i--) { var elmLink = document.links[i]; if (elmLink.href && elmLink.href.indexOf('javascript:') == -1) { elmLink.addEventListener('mouseover', mouseover, true); elmLink.addEventListener('mouseout', mouseout, true); elmLink.addEventListener('click', click, true); } } 

2.9.2. Running the Hack

Before running this hack, you'll need to set up Firefox so that it doesn't bring new tabs to the front. Go to Tools Options Advanced. Under Tabbed Browsing, make sure "Select new tabs opened from links is not checked, as shown in Figure 2-14.

Figure 2-14. Firefox tab options


Now, install the user script (Tools Install This User Script), and go to http://del.icio.us/popular/. Hover over any link for a short time (1.5 seconds to be precise), and the link will open in a new tab in the background, as shown in Figure 2-15.

Figure 2-15. Auto-opened link


The script is smart enough not to reopen links you've already opened. If you move your cursor away from the link you just opened, then move it back to the link, it will not open a second time.

The script is also smart enough not to auto-open links you've already clicked. If you move to another link and click while holding down the Ctrl key (or the Command key on Mac OS X), Firefox will open the link in a new tab in the background. If you move away from the link and then move back, it will not auto-open no matter how long you hover over it.

     < Day Day Up > 


    Greasemonkey Hacks
    Greasemonkey Hacks: Tips & Tools for Remixing the Web with Firefox
    ISBN: 0596101651
    EAN: 2147483647
    Year: 2005
    Pages: 168
    Authors: Mark Pilgrim

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net