Monday, 16 May 2011

Farnell Box Ticker

Frequent users of the Farnell website may, like me, get annoyed at having to continually tick the 'in stock', 'RoHs' and 'Exclude extended range' check boxes while searching for parts. Unfortunately, these check boxes do not seem to stay checked beyond a single search, even if you log in it will not save your preferences.

Using Greasemonkey to tick Farnell Search Boxes



So I set about writing a greasemonkey script to automatically tick these boxes when the page loads. Just in case I later decided I did want to pay £16 to ship a resistor across the Atlantic, I decided to add a keyboard short cut to invert the selection.

It turns out that greasemonkey is really annoying. User scripts execute in a sandbox, which means you can't access elements on the page or in the DOM as you would in say Firebug or a script running on the page itself. There is quite a lot of documentation out there about all these quirks, but even simple things like changing the value of a checkbox becomes a real headache.

After spending an evening getting to grips with Greasemonkey and refreshing my Javascript foo, I thought I had it cracked. So much so that I uploaded my efforts and boasted about it on Twitter. Then I took a closer look and discovered that all my script actually achieved, was graphically ticking the boxes on the Farnell search page and not actually affecting the search.

A little more time spent inserting breakpoints into the Farnell application script and I found the function that needed to be called to affect the search. For this, Firebug was essential! From the DOM tab, you can right click on an element and select 'Break on attribute change'.

Installing the script


The working script can be downloaded from userscripts, it is called: Farnell Box Ticker or you can directly install it by clicking this link

Unlike many scripts, this does not use the potentially unsafe 'Unsafe Window' work-around. Instead it relies on the fact you can type javascript directly into the URL box in your browser like thus: javascript:alert('beesnotincluded"');


Using the script


Once installed, this script will run on all of the farnell domains (hopefully). On the non-parametric search pages, the Stock,ROHS,Extended items boxes will automatically be ticked. For parametric searches, these boxes cannot be automatically ticked, because they are generated dynamically after the page has loaded. You can however, use the CTRL+E shortcut to toggle the state of these boxes on these pages.

Bug reports welcomed :s

3 comments:

Gavin said...

Very handy! Though I doubt the shortcut will get much use...

Why do you want to tick the RoHS checkbox too? Most things these days are lead free anyway, so I don't really bother with that one.

Would have saved a big headache during GDP if you had installed it onto Robbie's machine... ;)

Chris said...

True, most components are RoHS nowadays, but for work it is important, as we like to be able to put Pb Free stickers on all our hardware.

I could do a re-spin without this option ticked if you think you'd use it. Ideally it would be nice to configure it via greasemonkey. By the time I got it working I couldn't be bothered to faff around with it!

Thanks for the comment!

Gavin said...

Nah it's cool, it doesn't really bother me as very little is non RoHS these days. Installed greasemonkey and gave it a go, it works! :)