-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgumRoadDev.js
157 lines (150 loc) · 5.09 KB
/
gumRoadDev.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
(function() {
const style = document.createElement('style');
style.id = 'gumroad-style';
style.innerHTML = `
a.gumroad-button {
background: #ffcf42;
border-radius: 3px;
color: #333;
display: inline-block;
font-weight: bold;
margin-right: 16px;
padding: 15px 15px;
text-decoration: none;
}
.gumroad-iframe-wrapper {
align-items: center;
background: rgba(0,0,0,.0);
cursor: pointer;
display:flex;
justify-content: center;
left:0;
position: fixed;
top:0;
transition: background .3s linear;
z-index: 99998;
}
.gumroad-iframe-wrapper.loaded,
.gumroad-iframe-wrapper.loading {
background: rgba(0,0,0,0.6);
height: 100%;
width: 100%;
}
.gumroad-iframe-wrapper.embed {
background: transparent;
cursor: initial;
display: block;
height: 573px;
margin: 0 auto;
max-width: 1024px;
position: initial;
}
.gumroad-iframe-wrapper.embed.loading {
height: 50px;
width: 100px;
}
.gumroad-iframe-wrapper.loading:before {
align-items: center;
background: #fff;
color: #333;
content: 'Loading';
display: flex;
justify-content: center;
padding: 10px 50px;
}
.gumroad-iframe-wrapper.embed.loading:before {
display: none;
}
.gumroad-iframe-wrapper.loaded .gumroad-iframe {
height: 564px;
width: 100%;
}
.gumroad-iframe {
max-width: 670px;
z-index:2;
border:none;
width:0;
height:0;
}
`;
document.head.appendChild(style);
})();
(function() {
const Gumroad = {
init: function() {
/**
* Let's first collect all the links to Gumroad products on the page.
*/
let gumroadLinks = document.querySelectorAll('a[href*="https://gum.co"]');
/**
* if there are no valid links to Gumroad, the user most likely made a typo
* let's be nice about it and let them know.
* Alerts are very intrusive but it gets the users attention
*/
if (gumroadLinks.length === 0) {
alert(
`You included Gumroad widget JavaScript but no valid product link was found.
\nRefer to https://gumroad.com/widgets to set it up properly.`
);
}
gumroadLinks = Array.from(gumroadLinks); // by default querySelectorAll doesn't return an array but rather a NodeList
gumroadLinks.forEach(gumroadLink => {
const productLink = gumroadLink.getAttribute('href');
const displayStyle = gumroadLink.getAttribute('data-display-style');
const iframeWrapper = Gumroad.createIframeWrapper(displayStyle);
const iframeToBeLoaded = Gumroad.createIframe(
gumroadLink,
iframeWrapper
);
/**
* Here is where we are handling whether it should be embeded directly,
* or wait for the user to click on the button and open it as an overlay,
* the attribute `data-display-style` dictates which style of display (overlay or embed) the user chose.
*/
if (displayStyle === 'embed') {
iframeWrapper.classList.add('loading', 'embed');
iframeToBeLoaded.setAttribute('src', productLink);
} else {
gumroadLink.addEventListener('click', event => {
event.preventDefault();
iframeWrapper.classList.add('loading');
iframeToBeLoaded.setAttribute('src', productLink);
});
}
});
},
createIframeWrapper: displayStyle => {
const iframeWrapper = document.createElement('div');
iframeWrapper.classList.add('gumroad-iframe-wrapper');
document.body.appendChild(iframeWrapper);
// if it is an overlay, clicking on the overlay should dismiss it.
if (displayStyle !== 'embed') {
iframeWrapper.addEventListener('click', event => {
iframeWrapper.classList.remove('loaded');
});
}
return iframeWrapper;
},
createIframe: (gumroadLink, iframeWrapper) => {
const gumroadIframe = document.createElement('iframe');
gumroadIframe.setAttribute('data-src', gumroadLink);
gumroadIframe.classList.add('gumroad-iframe');
//setup iframe load event listener
gumroadIframe.addEventListener('load', () => {
const iframeSrc = gumroadIframe.getAttribute('src');
if (iframeSrc) {
iframeWrapper.classList.remove('loading');
iframeWrapper.classList.add('loaded');
}
// after it is done loading if it is an embedable form hide the source link
const displayStyle = gumroadLink.getAttribute('data-display-style');
if (displayStyle === 'embed') {
gumroadLink.style.display = 'none';
}
});
iframeWrapper.appendChild(gumroadIframe);
return gumroadIframe;
}
};
Gumroad.init();
})();