
// Amazon Affiliate Anti-Hijacker
// version 0.3 BETA!
// 2005-04-21
//
// --------------------------------------------------------------------
//
// Browser extensions, toolbars, and Greasemonkey user scripts can
// hijack your Amazon affiliate links.  This script tries to restore
// any damage done by such scripts.
//
// To use it, edit the line below that starts "var myAffiliateID ="
// to define your Amazon affiliate ID.  Then copy this script to
// your web server and link to it from each page on your site:
//
// <script type="text/javascript" src="/path/to/your/copy/of/antihijacker.js"></script>
//

/* 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 */

var _onload;
if (window.onload) {
    _onload = window.onload;
}
window.onload = (function() {
    var myAffiliateID = 'YOUR_AMAZON_AFFILIATE_ID_GOES_HERE';

    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 restoreAmazonLink(a, myAffiliateID) {
        var href, asin;
        href = a.href;
        asin = getASIN(href);
        if (!asin) { return; }
        a.href = 'http://' + getDomain(href) + '/exec/obidos/ASIN/' +
            asin + '/ref=nosim/' + myAffiliateID;
    }

    var links, i, a;
    links = document.getElementsByTagName("a");
    for (i = 0; i < links.length; i += 1) {
        a = links[i];
        if (a.href.match(/\/exec\/obidos\/ASIN\/(\w{10})/i)) {
            if (!a.href.match(myAffiliateID)) {
                restoreAmazonLink(a, myAffiliateID);
            }
        }
    }
    if (_onload) {
        _onload();
    }
});

//
// ChangeLog
// 2005-04-21 - 0.3 - MAP - linted
// 2005-04-18 - 0.2 - MAP - tidy code
//
