Skip to content

Instantly share code, notes, and snippets.

@stephenturner
Last active March 31, 2016 19:16
Show Gist options
  • Save stephenturner/af69ba0cee49d31c26f7 to your computer and use it in GitHub Desktop.
Save stephenturner/af69ba0cee49d31c26f7 to your computer and use it in GitHub Desktop.
Shorten Amazon Links (with affiliate ID) for Google Chrome
// ==UserScript==
// @name Shorten Amazon Links (with affiliate ID) for Google Chrome
// @namespace http://userscripts.org/users/427315
// @description Shorten all links to Amazon products for easy emailing. (Strips Amazon associate referral tags and replaces with gettgenedone-20.)
// This is just a modification of Callum Locke's script at http://userscripts.org/scripts/show/55185
// ...but adjusted so it will work for google chrome
// On Chrome, manually set the associateID below, as greasemonkey (GM_) functions won't work.
// On chrome, must start with --enable-easy-off-store-extension-install
// Then drag/drop to chrome://extensions
// @include *
// @author Kelly Nielsen (updated by Stephen Turner, gettgenedone-20)
// ==/UserScript==
var associateID = 'gettgenedone-20';
function getASIN(href) {
var asinMatch;
asinMatch = href.match(/\/exec\/obidos\/ASIN\/(\w{10})/i);
if (!asinMatch) { asinMatch = href.match(/\/gp\/product\/(\w{10})/i); }
if (!asinMatch) { asinMatch = href.match(/\/exec\/obidos\/tg\/detail\/\-\/(\w{10})/i); }
if (!asinMatch) { asinMatch = href.match(/\/dp\/(\w{10})/i); }
if (!asinMatch) { return null; }
return asinMatch[1];
}
function getDomain() {
if (document.location.hostname.substr(0,4) == 'www.')
return document.location.hostname.substr(4) ;
return document.location.hostname ;
}
(function() {
var allLinks = document.getElementsByTagName("a");
var asin = '';
var currentDomain = getDomain();
var linkDomain = (currentDomain.match(/amazon\./i) ? currentDomain : "amazon.com");
for (i = 0; i < allLinks.length; i++) {
var href = allLinks[i].href;
if (href.match(/amazon\./i)) {
asin = getASIN(href);
if (asin != null) {
allLinks[i].setAttribute("href", "http://"+linkDomain+"/o/ASIN/" + asin + "/ref=nosim/"+associateID);
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment