Skip to content

Instantly share code, notes, and snippets.

@jdorfman
Created November 9, 2019 05:17
Show Gist options
  • Save jdorfman/4edac86a9702accf5d64a232f99feb96 to your computer and use it in GitHub Desktop.
Save jdorfman/4edac86a9702accf5d64a232f99feb96 to your computer and use it in GitHub Desktop.
(function() {
function embed () {
var evt = new Event('codefund');
<% if @advertisement_html -%>
var uplift = {};
function trackUplift() {
try {
var url = '<%= @uplift_url.html_safe %>';
console.log('CodeFund is recording uplift. ' + url);
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.send();
} catch (e) {
console.log('CodeFund was unable to record uplift! ' + e.message);
}
};
function verifyUplift() {
if (uplift.pixel1 === undefined || uplift.pixel2 === undefined) { return; }
if (uplift.pixel1 && !uplift.pixel2) { trackUplift(); }
}
function detectUplift(count) {
var url = '<%= ENV["ADBLOCK_PLUS_PIXEL_URL"].to_s.strip %>';
if (url.length === 0) { return; }
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
if (count === 1) { detectUplift(2); }
uplift['pixel' + count] = true;
} else {
uplift['pixel' + count] = false;
}
verifyUplift();
}
};
xhr.open('GET', url + '?ch=' + count + '&rnd=' + Math.random() * 11);
xhr.send();
}
function elementVisible(element) {
if (!element) { return false; }
while (element) {
var style = getComputedStyle(element);
if (style.visibility === 'hidden' || style.display === 'none') { return false; }
element = element.parentElement;
}
return true;
}
function closeCodeFund() {
var codeFundElement = document.getElementById('codefund') || document.getElementById('<%= @target %>');
codeFundElement.remove();
sessionStorage.setItem('codefund', 'closed');
}
try {
if (sessionStorage.getItem('codefund') === 'closed') { return; }
var codeFundElement = document.getElementById('codefund') || document.getElementById('<%= @target %>');
if (!elementVisible(codeFundElement)) {
console.log('CodeFund element not visible! Please verify an element exists with id="codefund" and that it is visible.');
return;
}
codeFundElement.innerHTML = '<%= @advertisement_html.html_safe %>';
codeFundElement.querySelector('img[data-src="impression_url"]').src = '<%= @impression_url.html_safe %>';
codeFundElement.querySelectorAll('a[data-href="campaign_url"]').forEach(function (a) { a.href = '<%= @campaign_url.html_safe %>'; });
var poweredByElement = codeFundElement.querySelector('a[data-target="powered_by_url"]');
if (poweredByElement) { poweredByElement.href = '<%= @powered_by_url.html_safe %>'; }
var closeElement = codeFundElement.querySelector('button[data-behavior="close"]');
if (closeElement) { closeElement.addEventListener('click', closeCodeFund); }
evt.detail = { status: 'ok', house: <%= @campaign.fallback? %> };
detectUplift(1);
} catch (e) {
console.log('CodeFund detected an error! Please verify an element exists with id="codefund". ' + e.message);
evt.detail = { status: 'error', message: e.message };
}
<% else -%>
console.log('CodeFund does not have an advertiser for you at this time.');
evt.detail = { status: 'no-advertiser' };
<% end -%>
document.removeEventListener('DOMContentLoaded', embed);
window.dispatchEvent(evt);
};
(document.readyState === 'loading') ? document.addEventListener('DOMContentLoaded', embed) : embed();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment