Advertisement
Viruthagiri

Untitled

Aug 8th, 2011
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // set your AFFILIATE IDs here. Leave blank any you do not have an account for.
  2. var arrAffiliates = {
  3.     'co.uk' : '',
  4.     'com'   : '',
  5.     'de'    : '',
  6.     'fr'    : '',
  7.     'ca'    : '',
  8.     'jp'    : ''
  9. }
  10.  
  11. /**
  12.  * search for any AMAZON product links and replace with a referral link to the user's local Amazon site
  13.  * @author Pete Williams
  14.  * @url http://petewilliams.info/blog/2009/07/javascript-amazon-associate-link-localiser/
  15.  */
  16. function checkAmazonLinks() {
  17.  
  18.     // set required VARIABLES
  19.     var strTld;
  20.     var objRegexAsin    = new RegExp( '\/([A-Z0-9]{10})' );
  21.  
  22.     if ( typeof google != "undefined" && google.loader.ClientLocation != null ) {
  23.         var strCountry      = google.loader.ClientLocation.address.country_code;
  24.  
  25.         // get domain TLD from country code
  26.         switch ( strCountry ) {
  27.             case 'GB':
  28.             case 'IE':
  29.                 strTld = 'co.uk';
  30.                 break;
  31.             case 'AT':
  32.                 strTld = 'de';
  33.             default:
  34.                 // by default, TLD is lowercase of country code. if no associate, then use .com
  35.                 strTld = ( arrAffiliates[strCountry.toLowerCase()] ? strCountry.toLowerCase() : 'com' );
  36.                 break;
  37.         }
  38.  
  39.         strAffiliateId = getAffiliateId( strTld );
  40.  
  41.     }
  42.  
  43.     // get all LINKS
  44.     var arrLinks = document.getElementsByTagName( 'a' );
  45.  
  46.     // LOOP through
  47.     for ( i=0; i<arrLinks.length; i++ ) {
  48.  
  49.         // is it an AMAZON link
  50.         var intIndex = arrLinks[i].href.toLowerCase().indexOf( 'amazon.' );
  51.  
  52.         if ( intIndex > 0) {
  53.  
  54.             // find ASIN
  55.             var arrResults = objRegexAsin.exec( arrLinks[i].href );
  56.  
  57.             if ( arrResults ) {
  58.                 // REPLACE URI
  59.                 var strAsin = arrResults[1];
  60.  
  61. // check location has worked
  62.                 if ( typeof(strAffiliateId) == "undefined" ) {
  63.                     strTldToUse         = getOriginalTld( arrLinks[i].href );
  64.                     strAffiliateIdToUse = getAffiliateId( strTldToUse );
  65.                 } else {
  66.                     strTldToUse     = strTld;
  67.                     strAffiliateIdToUse = strAffiliateId;
  68.                 }
  69.  
  70.                 arrLinks[i].href  = 'http://www.amazon.' + strTldToUse + '/exec/obidos/ASIN/' + strAsin + '/' + strAffiliateIdToUse;
  71.             }
  72.  
  73.  
  74.         }
  75.     }
  76. }
  77.  
  78. function getAffiliateId( strTLD ) {
  79.     return( arrAffiliates[strTLD] ? arrAffiliates[strTLD] : arrAffiliatesSpares[strTLD] );
  80. }
  81.  
  82. function getOriginalTld( strUrl ) {
  83.     var c = new RegExp( "amazon.(.*?)/" );
  84.     arrTldActual = c.exec( strUrl );
  85.  
  86.     return arrTldActual[1];
  87. }
  88.  
  89. // initialise on page LOAD
  90. if (window.addEventListener){
  91.     window.addEventListener('load',checkAmazonLinks,false); //W3C
  92. } else{
  93.     window.attachEvent('onload',checkAmazonLinks); //IE
  94. }
  95.  
  96. // these are the author's affiliate IDs. They will only be used for localities you do not provide an ID for.
  97. var arrAffiliatesSpares = {
  98.     'co.uk' : 'pcrev05',
  99.     'com'   : 'petewill-20',
  100.     'de'    : 'petewill05-21',
  101.     'fr'    : 'petewill-21',
  102.     'ca'    : 'petewill00-20',
  103.     'jp'    : 'petewill-22'
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement