-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (80 loc) · 3.08 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
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
/*
Wordle import/export
By James Martin (@ymhr)
Pretty simple - it copies the stats out of local storage, then the user is responsible to getting it
from one device to the other, then it provides a method to paste and import it.
*/
const statsKey = 'nyt-wordle-statistics';
function createModal() {
const modal = document.createElement('div');
modal.style.background = '#fff';
modal.style.padding = '10px';
modal.style.position = 'relative';
const copyButton = document.createElement('button');
copyButton.innerText = 'Copy stats to clipboard';
copyButton.onclick = copyStatsToClipboard.bind(null, modal);
const importButton = document.createElement('button');
importButton.innerText = 'Import';
importButton.onclick = replaceStatsInLocalStorage;
modal.innerHTML =
"<h1>Import/export stats</h1><div>Click the 'copy to clipboard' button below to copy your stats. Paste them somewhere safe. Then, on your new device, copy that text to your clipboard and click 'import from clipboard'.<br><br>This will OVERWRITE the stats currently on the device and replace it with the ones you previously exported, so please <strong> USE WITH CAUTION</strong>.</div>";
modal.append(copyButton, importButton);
const closeButton = document.createElement('div');
closeButton.onclick = () => window.location.reload();
modal.prepend(closeButton);
closeButton.innerHTML = '<strong>X</strong>';
closeButton.style.cssText =
'position:absolute;top:0;right:0;cursor:pointer;padding:5px';
return modal;
}
/**
*
* @param {HTMLDivElement} modal
*/
function copyStatsToClipboard(modal) {
const statsData = localStorage.getItem(statsKey);
if (!statsKey) {
alert('No stats found!');
}
const hiddenInput = document.createElement('input');
hiddenInput.value = statsData;
hiddenInput.style.opacity = 0;
modal.append(hiddenInput);
hiddenInput.select();
document.execCommand('copy');
alert('Copied! Now put it somewhere safe');
}
function replaceStatsInLocalStorage() {
let message = 'Paste your stats here to replace them on this device.';
if (localStorage.getItem(statsKey)) {
message =
'There are already stats on this device. Are you sure you wish to replace them? This cannot be undone.';
}
/*
I'd rather use `execCommand('paste')` but it's disabled on most devices.
There's an alternative modern version but it requires granting permissions and I did not want to do that on someone elses website,
so I made it manual instead.
*/
const stats = prompt(message);
if (stats) {
try {
//Parsing here to make sure these stats are valid JSON
JSON.parse(stats);
localStorage.setItem(statsKey, stats);
alert('Stats successfully imported. Have fun!');
window.location.reload();
} catch (e) {
alert('These stats are invalid!');
}
} else {
alert('Aborted. Nothing has been changed.');
}
}
(() => {
const container = document.createElement('div');
container.style.cssText =
'display:flex;position:absolute;top:0;right:0;left:0;bottom:0;justify-content:center;align-items:center;background:rgba(0,0,0,0.7)';
const modal = createModal();
container.append(modal);
document.body.after(container);
})();