Skip to content

Instantly share code, notes, and snippets.

@camilleroux
Created July 25, 2014 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save camilleroux/26a99dfa63c123bddbce to your computer and use it in GitHub Desktop.
Save camilleroux/26a99dfa63c123bddbce to your computer and use it in GitHub Desktop.
Modified and fixed Affililink
var addTagToEnd, universalCode;
universalCode = {
'amazon.co.uk': 'tag=',
'amazon.com': 'tag=',
'amazon.de': 'tag=',
'amazon.fr': 'tag=pragmaticentrepreneurs-21',
'javari.co.uk': 'tag=',
'javari.de': 'tag=',
'javari.fr': 'tag=pragmaticentrepreneurs-21',
'amazonsupply.com': 'tag=',
'amazonwireless.com': 'tag=',
'endless.com': 'tag='
};
addTagToEnd = function (url) {
var link, match, match2, tag, domain;
domain = url.split("/")[2];
for (link in universalCode) {
tag = universalCode[link];
if (!(domain === link || domain.substring(domain.length - link.length - 1) === '.' + link)) {
continue;
}
if (!(link && tag)) {
return url; //no change
}
match = tag.match(/([a-zA-Z0-9\-]+)=([a-zA-Z0-9\-]+)/);
if (!match[2]) { //if tag is not configured
return url; //no change
}
match2 = new RegExp(match[1] + '=([a-zA-Z0-9\-]+)');
if (url.search(match2) > -1) {
url = url.replace(match2, match[1] + '=' + match[2]);
return url;
}
if (url.substring(url.length, url.length - 1) === '/') {
url += '?' + match[1] + '=' + match[2];
return url;
}
if (url.match(/(\?)/)) {
url += '&' + match[1] + '=' + match[2];
} else {
url += '/?' + match[1] + '=' + match[2];
}
return url;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment