Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          Metafilter Amazon Affiliate Redirector
  3. // @namespace    
  4. // @description   Plugs Amazon Links To A Designated Affiliate
  5. // @include       *
  6. // @run-at        document-end
  7. // @version       1.5
  8. // @note          Pretty much entirely github.com/jslabaugh/Amazon-Affiliate-Redirector
  9. // ==/UserScript==
  10.  
  11. // Define Your Affiliate ID
  12. var affID = 'metafilter-20';
  13.  
  14. // Get The Amazon Product ID
  15. function getASIN(href) {
  16.   var asinMatch;
  17.   asinMatch = href.match(/\/exec\/obidos\/ASIN\/(\w{10})/i);
  18.   if (!asinMatch) { asinMatch = href.match(/\/gp\/product\/(\w{10})/i); }
  19.   if (!asinMatch) { asinMatch = href.match(/\/exec\/obidos\/tg\/detail\/\-\/(\w{10})/i); }
  20.   if (!asinMatch) { asinMatch = href.match(/\/dp\/(\w{10})/i); }
  21.   if (!asinMatch) { return null; }
  22.   return asinMatch[1];
  23. }
  24.  
  25. // Get The Domain
  26. function getDomain() {
  27.   if (document.location.hostname.substr(0,4) == 'www.') {
  28.     return document.location.hostname.substr(4) ;
  29.   }
  30.   return document.location.hostname ;
  31. }
  32.  
  33. // The Main Function
  34. (function() {
  35.  
  36.   // Scope
  37.   var asin = '';
  38.   var currentDomain = getDomain();
  39.   var linkDomain = (currentDomain.match(/amazon\./i) ? currentDomain : "amazon.com");
  40.  
  41.   // Get All Of Our Links And Loop
  42.   var allLinks = document.getElementsByTagName("a");
  43.   for (i = 0; i < allLinks.length; i++) {
  44.     var href = allLinks[i].href;
  45.     if (href.match(/amazon\./i)) {
  46.       asin = getASIN(href);
  47.       if (asin != null) {
  48.         allLinks[i].setAttribute("href", "http://"+linkDomain+"/o/ASIN/" + asin + "/ref=nosim/"+affID);
  49.       }
  50.     }
  51.   }
  52. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement