-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
73 lines (62 loc) · 2.19 KB
/
index.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
import './index.scss';
import icon from '../img/icon.svg';
const genTipDeeplink = ({ url, ...params }) => {
const u = new URL('https://wallet.superhero.com/tip');
u.searchParams.set('url', url);
u.searchParams.set('x-success', url);
u.searchParams.set('x-cancel', url);
Object.entries(params)
.filter(([, value]) => ![undefined, null].includes(value))
.forEach(([name, value]) => u.searchParams.set(name, value));
return u;
};
let tips;
const getTipAmount = async (url) => {
if (!tips) {
tips = (await (await fetch('https://raendom-backend.z52da5wt.xyz/cache/stats')).json()).by_url;
}
return tips.find(u => u.url === url)?.total_amount_ae || 0;
};
export const createButtonByDiv = (divElement, { size = 'icon', url = window.location.href, account, ...options }) => {
// data-account attribute is needed claiming
const genLink = (text = '') => `
<a
href="${genTipDeeplink({ url, ...options })}"
${account ? `data-account="${account}"` : ''}
>
<img alt="Superhero Icon" src="${icon}" />
${text && `<span>${text}</span>`}
</a>`;
const tipsAmount = `
<div class="tips-amount">
<span class="tips">0</span>
<span class="ae">AE</span>
</div>`;
const templates = {
icon: genLink(),
small: genLink('Donate') + tipsAmount,
medium: genLink('Donate Now') + tipsAmount,
large: tipsAmount + genLink('Donate Now'),
};
if (!templates[size]) throw new Error('Unsupported size');
divElement.innerHTML = templates[size];
divElement.className = `superhero-utils-button ${size}`;
(async () => {
const tipsEl = divElement.querySelector('.tips');
if (tipsEl) tipsEl.innerHTML = await getTipAmount(url);
})();
return divElement;
};
export default (selectorOrElement, options = {}) => {
const element = typeof selectorOrElement === 'string'
? document.querySelectorAll(selectorOrElement)
: selectorOrElement;
const handleElement = element => {
const instance = createButtonByDiv(document.createElement('div'), options);
element.replaceWith(instance);
return instance;
};
return NodeList.prototype.isPrototypeOf(element)
? Array.from(element).map(handleElement)
: handleElement(element);
};