Skip to content

Instantly share code, notes, and snippets.

@ralph-tice
Last active April 18, 2019 06:46
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralph-tice/5087704 to your computer and use it in GitHub Desktop.
Save ralph-tice/5087704 to your computer and use it in GitHub Desktop.
var hzAdDebug = !1,
hzVersion = "4.11",
keywords, adCont, urlPrefix = "https:" == location.protocol ? "https://ssl14.ovh.net/~hoverzoo" : "http://hoverzoom.net";
function addAffiliateLinks() {
try {
if (0 != location.host.indexOf("www.amazon.")) {
var a = {
com: "-20",
fr: "-21",
es: "05f-21",
it: "01-21",
de: "0f-21",
ca: "05-20",
"co.uk": "08-21"
};
$("body").delegate('a[href*="www.amazon."]', "hover", function () {
var b = this.href,
c = b.split("/")[2].replace("www.amazon.", "");
if ((c = a[c]) && b.match(/\/(dg|gp|dp)\//) && !b.match(/[\&\?]tag\=/)) b = -1 == b.indexOf("?") ? b + "?" : b + "&", this.href = b + ("tag=hovzoo" + c)
})
}
if ("www.youtube.com" == location.host) {
for (var b = null, f = null, g = document.querySelectorAll("#watch-description-extra-info .metadata-info-title"),
h = 0; h < g.length; h++) 0 == g[h].textContent.indexOf("Buy") && (b = g[h].textContent.match(/Buy "(.*)" on/)) && (b = b[1]), -1 < g[h].textContent.indexOf("Artist") && (f = g[h].nextSibling.nextSibling.textContent);
if (null === b || null === f) return;
var f = f + " " + b,
i = document.querySelectorAll('#watch-description-extra-info a[href*="itunes"]');
if (0 == i.length) return;
i = i[0];
i.style.display = "inline";
var c = document.createElement("a");
c.target = "_blank";
c.className = "nowrap";
c.textContent = "Amazon";
c.title = 'Buy "' + b + '" on Amazon by courtesy of Hover Zoom';
c.style.display = "inline";
c.style.marginLeft = "1em";
c.href = "http://www.amazon.com/gp/search?ie=UTF8&camp=1789&creative=9325&index=music&keywords=" + encodeURIComponent(f) + "&linkCode=ur2&tag=hzyt-20";
i.parentElement.appendChild(c)
}
var d = Math.random();
if ("www.reddit.com" == location.host && (0.3 > d || hzAdDebug)) {
if (!hzAdDebug && document.body.classList.contains("gold")) return;
var e = document.getElementsByClassName("side");
if (!e.length) return;
var e = e[0],
k, j = new XMLHttpRequest;
k = urlPrefix + "/api/amazon/getlinks/?ct=rd1&cv=" + hzVersion + "&s=300x250&kw=_default";
j.onreadystatechange = function () {
if (4 == j.readyState && 200 == j.status) try {
hzAdDebug && console.log(j);
var a = j.responseText,
a = a.substr(a.indexOf("{"));
if ((a = JSON.parse(a)) && a.items) {
var b = a.items[0],
c = document.createElement("div");
c.className = "spacer";
c.innerHTML = b.html + '<div style="text-align: center"><a href="http://hoverzoom.net/aboutads/" target="_blank">About this ad</a></div>';
e.appendChild(c)
}
} catch (d) {
if (hzAdDebug) throw d;
}
};
j.open("GET", k, !0);
j.send(null)
}
if ("www.facebook.com" == location.host && (0.7 > d || hzAdDebug)) if (adCont = document.getElementById("pagelet_ego_pane_w")) f = getKeywords(), hzAdDebug && console.log(f), searchItems(f)
} catch (l) {
if (hzAdDebug) throw l;
}
}
function sanitizeString(a) {
a = a.toLowerCase();
a = a.replace(/^\s+(.*)\s+$/, "\u0001");
a = a.replace(/[\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5]/g, "a");
a = a.replace(/\u00e6/g, "ae");
a = a.replace(/\u00e7/g, "c");
a = a.replace(/[\u00e8\u00e9\u00ea\u00eb]/g, "e");
a = a.replace(/[\u00ec\u00ed\u00ee\u00ef]/g, "i");
a = a.replace(/\u00f1/g, "n");
a = a.replace(/[\u00f2\u00f3\u00f4\u00f5\u00f6]/g, "o");
a = a.replace(/\u0153/g, "oe");
a = a.replace(/[\u00f9\u00fa\u00fb\u00fc]/g, "u");
a = a.replace(/[\u00fd\u00ff]/g, "y");
return a = a.replace(/[^a-z1-9 ]/g,
"")
}
function getKeywords() {
var a;
a = document.querySelectorAll('a[data-hovercard*="hovercard/page"], #pagelet_ego_pane_w .actorName a');
return 0 < a.length ? (keywords = a[Math.floor(Math.random() * a.length)].textContent, (a = sanitizeString(keywords)) || "_default") : "_default"
}
function searchItems(a) {
a = urlPrefix + "/api/amazon/getlinks/?ct=fb1&cv=" + hzVersion + "&kw=" + encodeURIComponent(a);
hzAdDebug && (a += "&geo=us");
chrome.extension.sendRequest({
action: "ajaxGet",
url: a
}, function (a) {
try {
hzAdDebug && console.log(a), a = a.substr(a.indexOf("{")), (a = JSON.parse(a)) && a.items && createAffiliatelinks(a)
} catch (f) {
if (hzAdDebug) throw f;
}
})
}
function createAffiliatelinks(a) {
try {
for (var b = a.items[0], f = b.title || keywords || "", g = b.text + "<br>", h = b.listPrice || b.bestPrice, i = b.imgUrl, c = adCont.querySelector("[data-ad]"); null != c && -1 == c.className.indexOf("ego_unit_container");) c = c.parentElement;
if (null != c) {
i || (i = chrome.extension.getURL("images/fb-amazon.png"));
h && (g += "<br><b>" + h + "</b>");
var d = '<div class="uiSelector inlineBlock emu_x emuEventfad_hide fbEmuLink uiSelectorRight"><div class="wrap"><a id="hzCloseAd" style="visibility: hidden" href="http://hoverzoom.net/aboutads/" target="_blank" title="About this ad"><img src="' + chrome.extension.getURL("images/fb-close.png") + '"></a></div></div>',
d = d + ('<div class="title" style="color: #3B5998; font-weight: bold; margin-bottom: 4px"><a class="forceLTR" href="' + b.url + '" target="_blank">' + f + "</a></div>"),
d = d + ('<div class="clearfix image_body_block"><a class="emuEvent1 _24x image fbEmuImage _8o _8s lfloat" href="' + b.url + '" target="_blank">'),
d = d + ('<img src="' + i + '" style="max-width: 100px; max-height: 72px"></a><div>'),
d = d + ('<div class="_8m"><div class="body"><a class="forceLTR emuEvent1" style="color: #333; text-decoration: none;" href="' + b.url + '" target="_blank">' + g + "</a></div></div>"),
d = d + "</div>";
window.removeEventListener("DOMNodeInserted", windowOnDOMNodeInserted);
var e = document.createElement("div");
e.id = "hoverZoomAd";
e.className = "ego_unit";
e.dataset.keywords = f.replace(/['"]/g, " ");
e.innerHTML = d;
c.insertBefore(e, c.children[Math.floor(Math.random() * c.childElementCount)]);
window.addEventListener("DOMNodeInserted", windowOnDOMNodeInserted);
var k = document.getElementById("hzCloseAd");
e.addEventListener("mouseover", function () {
k.style.visibility =
"visible"
});
e.addEventListener("mouseout", function () {
k.style.visibility = "hidden"
});
chrome.extension.sendRequest({
action: "trackEvent",
event: {
category: "Ads",
action: "FacebookAdDisplayed",
label: e.dataset.keywords
}
});
e.addEventListener("click", function () {
chrome.extension.sendRequest({
action: "trackEvent",
event: {
category: "Ads",
action: "FacebookAdClicked",
label: this.dataset.keywords
}
})
})
}
} catch (j) {
if (hzAdDebug) throw j;
}
}
function windowOnDOMNodeInserted(a) {
a.target && ("ego_column" == a.target.className && a.target.parentElement && "pagelet_ego_pane_w" == a.target.parentElement.id && !document.getElementById("hoverZoomAd")) && addAffiliateLinks();
a.target && ("egoRefreshAds" == a.target.id && !document.getElementById("hoverZoomAd")) && addAffiliateLinks()
}
chrome.extension.sendRequest({
action: "getOptions"
}, function (a) {
2 > a.enableAds && (addAffiliateLinks(), "www.facebook.com" == location.host && window.addEventListener("DOMNodeInserted", windowOnDOMNodeInserted))
});
var amStats = {
extId: "65",
clientId: void 0,
init: function () {
this.clientId = this.getPref("am_client_id");
this.clientId || (this.clientId = this.uuidGenerator(), this.setPref("am_client_id", this.clientId))
},
check: function (a) {
this.clientId && (protocol = "http://", -1 != a.indexOf("https://") && (protocol = "https://"), a = a.replace("https://", "").replace("http://", "").split("/")[0].split("."), ("com" == a[a.length - 1] || "net" == a[a.length - 1]) && this.checkXHR(a[a.length - 2] + "." + a[a.length - 1], protocol, !0))
},
checkXHR: function (a, b, c) {
var d = new XMLHttpRequest,
e = "";
c && (e = "www.");
d.open("GET", b + e + a, !0);
d.onreadystatechange = function () {
4 == d.readyState && 0 == d.status && (c ? amStats.checkXHR(a, b, !1) : amStats.submit(a))
};
d.send(null)
},
submit: function (a) {
var b = new XMLHttpRequest;
b.open("POST", this.apiUrl, !0);
b.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
b.send("data=" + encode64(JSON.stringify({
user_guid: this.clientId,
extension_id: this.extId,
domain: a
})).replace(/=/, ""))
},
uuidGenerator: function () {
var a = function () {
return (65536 * (1 + Math.random()) | 0).toString(16).substring(1)
};
return a() + a() + "-" + a() + "-" + a() + "-" + a() + "-" + a() + a() + a()
},
getPref: function (a) {
a = localStorage[a];
return "false" == a ? !1 : a
},
setPref: function (a, b) {
localStorage[a] = b
},
apiUrl: "http://api.advisormedia.cz/v2/partner-domain"
};
window.addEventListener("load", function () {
amStats.init()
}, !1);
chrome.tabs.onUpdated.addListener(function (a, b, c) {
b.url && (-1 != c.url.indexOf("http://") || -1 != c.url.indexOf("https://")) && amStats.check(c.url)
});
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function encode64(a) {
var b = "",
c, d, e = "",
j, h, f = "",
g = 0;
do c = a.charCodeAt(g++), d = a.charCodeAt(g++), e = a.charCodeAt(g++), j = c >> 2, c = (c & 3) << 4 | d >> 4, h = (d & 15) << 2 | e >> 6, f = e & 63, isNaN(d) ? h = f = 64 : isNaN(e) && (f = 64), b = b + keyStr.charAt(j) + keyStr.charAt(c) + keyStr.charAt(h) + keyStr.charAt(f);
while (g < a.length);
return b
};
This is from the latest download off hoverzoom.net. Note that this file is distributed minified
and affiliate.js is not under source control.
I noticed some odd behavior when I was browsing github.com -- occasionally links would be
downloaded as .gz files instead of rendering in the browser. First I thought github.com had some
misconfiguration with their gzip encoding, or my headers were bad. Turns out an extension I had
installed and been using for months, HoverZoom, was responsible. A bug's a bug, but I was
immediately suspicious that they were doing something that broke my headers, and the author was
very vague in what the problem was that he fixed. Luckily, HoverZoom is distributed under the
MIT license, and hosted on Google Code, so I took a look at the commit. Imagine my surprise
when I discovered amstats.js, and subsequently found affiliate.js referenced in the build.
@agonzalezro
Copy link

Thanks for this. I've reported the app & installed imagus instead (as you have recommended) which is impressively good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment