Skip to content

Instantly share code, notes, and snippets.

@stephenturner
Created November 21, 2017 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenturner/7d306a73dc7c1c9747a91f69319b2b09 to your computer and use it in GitHub Desktop.
Save stephenturner/7d306a73dc7c1c9747a91f69319b2b09 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Use My Amazon Affiliate Code
// @namespace http://www.lysator.liu.se/~jhs/userscript
// @description Puts your own Amazon affiliate ID in Amazon links
// @include http://*
// @include https://*
// ==/UserScript==
// leave intact to sponsor my scripting, or set to your own:
const affiliate = 'gettgenedone-20';
const at_amazon = /(.*\.)?amazon\.(com|[a-z]{2}(\.[a-z]{2})?)$/i;
const aff_links = /(obidos.(ASIN.{12}([^\/]*(=|%3D)[^\/]*\/)*|redirect[^\/]*.(tag=)?))[^\/&]+/i;
if( location.hostname.match( at_amazon ) )
return;
var i, l, p;
for( i=0; l=document.links[i]; i++ )
{
if( !l.hostname.match( at_amazon ) || !l.hostname.match( /^www\./i ) )
continue;
if( l.href.match( aff_links ) )
l.href = l.href.replace( aff_links, '$1'+affiliate );
else if( !l.search )
{
p = encodeURIComponent( l.pathname.substring( 1 ) + l.search );
l.search = '?tag='+ affiliate +'&path='+ p;
l.pathname = '/exec/obidos/redirect';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment