Thursday, December 11, 2008

Bookmarklet to add recycle comment in GreenMetropolis

I'm selling a fair number of my old paperbacks on GreenMetropolis.com. When listing a book I invariably want to note that I will send in recycled packaging in the comments field. I was using iMacro to speed this up but I've finally got around to creating a simple bookmarklet to do the same thing. Here it is:

javascript:(
function(){
var cmts=document.getElementsByTagName('TEXTAREA');
for(var ic=0;ic<cmts.length;ic++){
if(cmts[ic].name=='comment'){
cmts[ic].value+='I will send with reused or recycled packaging.'
}
}
}
)()

And here it is as a link you can drag and drop into your own bookmarks or try it out by clicking on it here: Add note to TextArea



Reset text area

This would be fairly easy to customize for your own text or to adapt to a different form that you use regularly and get tired of typing in the same old text or if you are familiar with RegEx tweaking the text you already have in the text box. For example changing the text in the text box to Title Case or Shout.

Tuesday, December 9, 2008

Creating an intelligent shortcut (bookmarklet) in Firefox

On of the frequent address shortcuts I use in Firefox is to redirect "w sometext" in the address bar to lookup a term (sometext) in Wikipedia. As I frequently go to my "Watchlist" in Wikipedia I though it would be neat to default to that page if I had not specified a term. A little research and I'd worked out how to get this to work as a bookmarklet. In the address bar any text added to the shortcut is referenced as "%s".

Using this basic structure it would be pretty easy to add a more complex set of conditions to make the same shortcut do many more things, such as looking up a book and working out if the text you put in was an ISBN or a title.

Here's my example formatted so you can read it:


javascript:(
function(){
  if("%s".length==0){
     // Default
    window.location="http://en.wikipedia.org/wiki/Special:Watchlist"
  }else{
     // Wikipedia lookup
    window.location="http://en.wikipedia.org/wiki/%s"
  }
}
)()


And here it is formatted as a link you can drop into your bookmarks, don't forget to edit it so that it has "w" as a keyword/shortcut:
Wikipedia lookup

Monday, December 1, 2008

Moving my mobile number from Virgin to the evil ASDA / Wal*mart empire

Sadly Virgin aren't doing all that much to compete with the johnny-cum-lately ASDA. ASDA are currently charging a flat rate for PAYG of 8p/min for all UK landline and all mobile calls with 4p per text message. Virgin claimed to charge 15p/min and 3p for texts on PAYG but in practice (as I don't know anyone using Virgin and my usage is so low it was never worth buying a "bundle") their charges were actually a whopping 40p/min and 12p/text.

It's a great pity but as my average bill even at these rates is always less than £5/month I can't afford to ignore the ASDA deal.

Transfer happened this weekend. I emailed Virgin via their website on Thursday, phoned ASDA with the transfer code on Friday and my mobile number was tranferred today. Surprisingly painless compared to the last time I had to do this (it took 3 weeks).

Sunday, November 16, 2008

Use Greasemonkey to link UK postcodes to Google Maps

See my last blog post for general details about using Greasemonkey. This titbit of source code uses a large standard regex to match any UK postcode in a web page text (you could be matching paragraph elements) and converts it into a hyperlink to a google map of that area. It assumes that this titbit is inside a script that searches relevant elements of the page and tests them as variable "thisElement".

// Convert postcodes to google map links (z=15 puts names on tube stations)
thisElement.innerHTML=thisElement.innerHTML.replace(/([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)/,"<a href='http://maps.google.com/maps?f=q&hl=en&geocode=&q=$1&ie=UTF8&z=15&iwloc=addr' target=_blank>$1</a>");

Friday, November 14, 2008

Write your own Greasemonkey script to highlight and hide items on a website

Here are some handy code segments that have taken me a little while to work out. Greasemonkey is a useful Firefox plugin with a massive shared library of scripts to do interesting things like adding collapsible folders to your online gmail web page. You can also write your own javascript code for Greasemonkey to apply to web sites you name.

The following code I use for a mystery shopping website I visit regularly but their table of possible assignments available in London can be several pages long. This script helps filter and colour in the table with my preferences (for example out of 80 London assignments I currently have 7 highlighted as suitable).

// ==UserScript==
// @name Website filter
// @namespace Filters
// @include http://mywebsite.com

/* Here I'm searching out items in a table that have been
highlighted in bold already. I'm then searching the bold
text and if it contains "William", I then hide the row
completely. I then go on to pick out items with "London"
in the text in dark green */


var allElements, thisElement;
allElements = document.evaluate(
'//tr/td/b',
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
for (var i = 0; i < allElements.snapshotLength; i++) {
thisElement = allElements.snapshotItem(i);
// Hide these rows
if(thisElement.innerHTML.search(/William/i)>=0) {
thisElement.parentNode.parentNode.style.display="none";
}
// Highlight these elements
if(thisElement.innerHTML.search(/London/i)>=0) {
thisElement.style.color="darkgreen";
}
}

/* In this section I'm searching for any table cell and
checking the text for certain postcodes and addresses.
If there is a match, the cell is highlighted in bold
blue text. */


allElements = document.evaluate(
'//td',
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
for (var i = 0; i < allElements.snapshotLength; i++) {
thisElement = allElements.snapshotItem(i);
// Highlight these addresses
if(thisElement.innerHTML.search(/SE(22|15|1\s)|London Bridge/i)>0) {
with(thisElement.style){
color="blue";
fontWeight="bold";
}
}
}

// ==/UserScript==

Explanations

Regular expressions (Regex) can be a bit cryptic but as you'll need to tailor these in the above code I though I'd explain what these bits do:
search(/William/i) ==> Search anywhere in the string for "William" and return the starting position within the string, the 'i' means that it will not be case sensitive.
search(/SE(22|15|1\s)|London Bridge/i) ==> Search anywhere in the string for "SE22", "SE15", "SE1 " or "London Bridge", ignore case and return the position.

You will also note that the first search that hides matching rows uses parentNode twice on the assumption that the bold text will be in a cell which will be in a row, so the second parent node is the "TR" tag containing the row. By changing the style.display attribute this dynamically hides the row from view (to reset and show the row again you would set style.display to null). Phew.

Useful links

Greasemonkey: https://addons.mozilla.org
XPath syntax: http://www.w3schools.com/XPath

Monday, November 10, 2008

iMacro script to swap between Flickr albums

The following handy iMacro script logs you from one Flickr account to another. I currently have six of these, one for each separate free Flickr album/account I have on the go (there's a maximum of 200 photos you can share for free in each account). All you have to do is install the iMacro plugin into your browser and save the following script not forgetting to add your login and password in the "User set variables" section.

Obviously if Flickr change their main site layout then this may break the script and it would have to be tweaked.

Script Starts

' Last Edit: 13 Nov 2008

' User set variables
' !VAR1 = your login ID, !VAR2 = your password

SET !VAR1 yourlogin@yahoo.com
SET !VAR2 password

' This script is intended to toggle you out of your
' current Flickr account and into one set by the
' variables above so you can easily swap between
' several albums

' Assumptions
' - A Sign Out link is on every Flickr page
' - You are currently logged into Flickr

' Main script

TAB T=1
FILTER TYPE=IMAGES STATUS=ON
SET !ERRORIGNORE YES
TAG POS=1 TYPE=A ATTR=TXT:Sign<SP>Out
TAG POS=1 TYPE=A ATTR=TXT:Sign<SP>in*
WAIT SECONDS=2
TAG POS=1 TYPE=A ATTR=TXT:Sign<SP>in<SP>as<SP>a*
SET !ERRORIGNORE NO
WAIT SECONDS=2
TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:login_form ATTR=NAME:login CONTENT={{!VAR1}}
SET !ENCRYPTION NO
TAG POS=1 TYPE=INPUT:PASSWORD ATTR=ID:passwd CONTENT={{!VAR2}}
TAG POS=1 TYPE=INPUT:SUBMIT ATTR=VALUE:Sign<SP>In
FILTER TYPE=IMAGES STATUS=OFF
URL GOTO=http://www.flickr.com/activity
TAG POS=1 TYPE=SELECT FORM=* ATTR=ID:act-since CONTENT=%lastlogin


Script Ends

Latest changes:
  1. I added the 2 second wait times as Sxipper (a password plugin) seemed to be causing 'focus' problems.
  2. ErrorIgnore is used so that the script still works (though slower) if you have logged off but are still on the Flickr site.

Wednesday, October 22, 2008

Leopard problems with OpenOffice

Since installing an update from Apple to my X11 application, OpenOffice has refused to run. Damned annoying that Apple's development labs can't test updates properly.

Luckily I found a solution on a technical forum of running NeoOffice instead. NeoOffice uses exactly the same open source as Open Office, so saved files are completely interchangeable with OpenOffice, with the odd additional feature thrown in. NeoOffice runs natively on Leopard and so doesn't rely on X11. So far it seems to run rather better than Open Office and I have had great fun mapping out my kitchen using the drawing application (very similar features to Visio) which includes the facility to draw to scale, so my kitchen units can be represented quite accurately for my workmen to follow.

Next time I'm running updates from Apple I'll research the forums before rashly saying yes to new bugs.