DealExtreme Referral Helper Bookmarklet
This was a tool I put together to get people to use my DealExtreme referral ID when they shop. If you want to tweak it for your own affiliate account, just change the first variable value.
by Adam Patridge
January 04, 2013
HTML
<p class="result"></p>
<script type="text/javascript">
var tools = {},
resultOutput;
tools.getByClassName = function(className) {
var allElements = document.getElementsByTagName('*'),
foundElements = [],
i;
for (i in allElements) {
if ((" " + allElements[i].className + " ").indexOf(" " + className + " ") > -1) {
foundElements.push(allElements[i]);
}
}
return foundElements;
};
resultOutput = tools.getByClassName("result")[0];
</script>
JavaScript
(function() {
var urlToProcess = "http://dx.com/p/430-g-17-tempered-glass-electronic-flashing-illuminated-erasable-led-message-writing-board-black-176991",
referralId = '58188542',
referralRootUrl = 'http://dx.com/?r=' + referralId,
getQuerystring = function(searchString) {
var i, kvp, result = {};
searchString = (searchString.indexOf("?") !== -1 ? searchString.substring(1) : searchString).split("&");
if (searchString === '') {
return {};
}
for (i = 0; i < searchString.length; i += 1) {
kvp = searchString[i].split('=');
if (kvp.length === 2) {
result[kvp[0]] = decodeURIComponent(kvp[1].replace(/\+/g, ' '));
}
}
return result;
},
affiliateUrl = (function(url) {
var location = document.createElement('a'),
isDealExtreme,
currentTag,
resultUrl;
location.href = url;
isDealExtreme= location.host.indexOf('dx.com') !== -1;
currentTag = getQuerystring((location.search && location.search.substring(1)) || '')['r'];
if (isDealExtreme && currentTag) {
resultUrl = url.replace(/([&?])r=[^&#]*/i, '$1r=' + referralId);
}
else if (isDealExtreme) {
// host includes port
resultUrl = location.protocol + '//' + location.host + location.pathname + (location.search ? location.search + '&' : '?') + 'Utm_rid=' + referralId + "&Utm_source=affiliate" + location.hash;
}
else {
resultUrl = referralRootUrl;
}
return resultUrl;
}(urlToProcess));
resultOutput.innerHTML = affiliateUrl;
// Cannot leave with window.location because DX does frame-popping
//window.location = affiliateUrl;
}());