
// Amazon Affiliate Hijacker 4
// version 4.3 BETA!
// 2005-04-21
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Amazon Affiliate Hijacker 4", and click Uninstall.
//
// --------------------------------------------------------------------
//
// MANDATORY CONFIGURATION:
// This user script operates on all non-Amazon pages, modifying links
// to Amazon to include the affiliate ID of your choice.  There is
// no default ID; you *must* edit the line of the script that starts
// with "var myAffiliateID =" to define your affiliate ID.  If you
// do not have your own affiliate ID, you can use the ID of a
// worthwhile charity, such as EFF ('electronicfro-20').
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          Amazon Affiliate Hijacker 4
// @namespace     http://diveintomark.org/projects/greasemonkey/
// @description   changes all Amazon links to include your affiliate ID
// @include       *
// @exclude       http://*.amazon.*/*
// ==/UserScript==

/* BEGIN LICENSE BLOCK
Copyright (C) 2005 Mark Pilgrim

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You can download a copy of the GNU General Public License at
http://diveintomark.org/projects/greasemonkey/COPYING
or get a free printed copy by writing to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
END LICENSE BLOCK */

function aahijack() {
    function getASIN(href) {
        var asinMatch, asin;
        asinMatch = href.match(/\/exec\/obidos\/ASIN\/(\w{10})/i);
        if (!asinMatch) { return null; }
        asin = asinMatch[1];
        return asin;
    }

    function getDomain(href) {
        return href.replace(/https?:\/\/(.+?)\/.*$/gi, '$1');
    }

    function changeAmazonLinks(myAffiliateID) {
        var amazonLinks, href, a, i, asin, domain;
        if (myAffiliateID.match(/\_/)) {
            alert(
'Amazon Affiliate Hijacker was not installed properly.  ' +
'Please go to Tools/Manage User Scripts and uninstall it, ' +
'then read the directions next time.');
            return false;
        }
        amazonLinks = document.evaluate(
            "//a[contains(translate(@href, 'AMAZON', 'amazon'), '.amazon.')]",
            document,
            null,
            XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
            null);
        for (i = 0; i < amazonLinks.snapshotLength; i += 1) {
            a = amazonLinks.snapshotItem(i);
            href = a.href;
            asin = getASIN(href);
            if (!asin) { continue; }
            domain = getDomain(href);
            a._href = 'http://' + domain + '/exec/obidos/ASIN/' + asin +
                '/ref=nosim/' + myAffiliateID;
            a.onclick = function() {
                window.location = this._href;
                return false;
            };
        }
        return true;
    }

    var myAffiliateID = 'YOUR_AMAZON_AFFILIATE_ID_GOES_HERE';
    if (changeAmazonLinks(myAffiliateID)) {
        window.setTimeout("aahijack()", 1);
    }
}

(function() {
    window.addEventListener("load", aahijack, true);
})();

//
// ChangeLog
// 2005-04-21 - 4.3 - MAP - linted
// 2005-04-18 - 4.2 - MAP - tidy code
//
