This repository has been archived by the owner on Aug 13, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
show.js.erb
94 lines (81 loc) · 3.5 KB
/
show.js.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(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();
})();