Skip to content

Instantly share code, notes, and snippets.

@coderberry
Created January 17, 2019 16:33
Show Gist options
  • Save coderberry/710e77e683d27bc3cd0503afdd934f8d to your computer and use it in GitHub Desktop.
Save coderberry/710e77e683d27bc3cd0503afdd934f8d to your computer and use it in GitHub Desktop.
(function() {
function embed () {
var evt = new Event('codefund');
<% if @advertisement_html -%>
try {
var targetElement = document.getElementById('codefund');
if (targetElement == null) { targetElement = document.getElementById('<%= @target %>'); }
targetElement.innerHTML = '<%= @advertisement_html.html_safe %>';
targetElement.querySelector('img[data-src="impression_url"]').src = '<%= @impression_url.html_safe %>&uplift=' + (window.abp === true);
targetElement.querySelectorAll('a[data-href="campaign_url"]').forEach(function (a) { a.href = '<%= @campaign_url.html_safe %>' });
evt.detail = { status: 'ok', house: <%= @campaign.fallback? %> };
} 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 a 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