Affiliate Helper Tool

Generates an affiliate URL and shortens it based on an array of known affiliate systems. To customize to your own use, change out the `affiliates` array as necessary and swap out your own URL-shortener API URL (`urlShortenerUrl`).

by Adam Patridge

HTML

<div>
    <p class="rawUrl"></p>
    <p class="affiliateUrl"></p>
    <div>
        <p class="shortUrl"></p>
        <a href="#" class="copyShortUrl">Copy</a>
    </div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!--<script type="text/javascript" src="http://www.steamdev.com/zclip/js/jquery.zclip.js"></script>-->

CSS

.copyShortUrl { display: none; }
body { padding: 10px; }

JavaScript

$(function() {
    var urlToProcess = prompt("Url to affiliatize?") || "http://www.newegg.com/",
        $rawUrl = $(".rawUrl"),
        $affiliateUrl = $(".affiliateUrl"),
        $shortUrl = $(".shortUrl"),
        $copyShortUrl = $(".copyShortUrl"),
        anchorOfUrl = (function(url) {
            var anchor = document.createElement("a");
            anchor.href = url;
            return anchor;
        }(urlToProcess)),
        AffiliateInfo = function(host, affiliateUrlBase, urlConverter) {
            this.host = host;
            this.affiliateUrlBase = affiliateUrlBase;
            this.urlConverter = urlConverter;
        },
        ebayUrlConverter = function(originalUrl) {
            var itemId = originalUrl;
            if (originalUrl.startsWith("https://www.ebay.com/itm/")) {
                var startIndex = "https://www.ebay.com/itm/".length;
                if (["1", "2", "3", "4", "5", "6", "7", "8", "9"].indexOf(originalUrl.charAt(startIndex)) < 0) {
                        // Has a name in the URL to trim out first, substring and move to next 
                        startIndex = originalUrl.indexOf("/", startIndex) + 1;
                }
                var endIndex = originalUrl.indexOf("?");
                if (endIndex < 0) {
                        endIndex = originalUrl.length - 1;
                }
                itemId = originalUrl.substring(startIndex, endIndex);
            };
            return "https://rover.ebay.com/rover/1/711-53200-19255-0/1?icep_ff3=2&pub=5575079428&toolid=10001&campid=5337996235&customid=&ipn=psmain&icep_vectorid=229466&kwid=902099&mtid=824&kw=lg&icep_item=" + itemId;
        },
        amazonUrlConverter = function (originalUrl) {
            affiliateId = "crazytechdeals-20";
            getQuerystring = function (b) {
                var a,
                    c,
                    d = {},
                    b = (b.indexOf("?") !== -1 ? b.substring(1) : b).split("&");
                if (b === "")...