forked from OpenZeppelin/docs.openzeppelin.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-hardhat-truffle-toggle.js
56 lines (47 loc) · 1.53 KB
/
06-hardhat-truffle-toggle.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
(function () {
window.addEventListener('click', e => {
if (!event.target.classList.contains('hardhat-truffle-toggle')) {
return;
}
togglePreference();
e.preventDefault();
});
const cookieName = 'hardhat_truffle_preference';
const paramName = 'pref';
const defaultPreference = 'hardhat';
function getCookie() {
const cookie = document.cookie.split('; ').find(c => c.startsWith(`${cookieName}=`));
return cookie && cookie.split('=')[1];
}
function setCookie(preference) {
document.cookie = `${cookieName}=${preference};path=/;max-age=31536000`;
}
function getURLParam() {
return new URLSearchParams(document.location.search).get(paramName);
}
function setURLParam(preference) {
const url = new URL(document.location);
const params = new URLSearchParams(url.search);
params.set('pref', preference);
url.search = params.toString();
history.replaceState(null, '', [url]);
}
function getPreference() {
const current = getURLParam() || getCookie();
if (current === 'hardhat' || current === 'truffle') {
return current;
} else {
return defaultPreference;
}
}
function togglePreference() {
const current = getPreference();
const other = current === 'hardhat' ? 'truffle' : 'hardhat';
document.body.classList.replace(`preference-${current}`, `preference-${other}`);
setCookie(other);
setURLParam(other);
}
const current = getPreference();
setCookie(current);
document.body.classList.add(`preference-${current}`);
})();