-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-on-click.js
50 lines (47 loc) · 1.71 KB
/
format-on-click.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
chrome.browserAction.onClicked.addListener(function () {
chrome.tabs.executeScript({
code: `
navigator.clipboard.readText()
.then((clipText) => {
try {
const jsonFormat = JSON.stringify(eval("(" + clipText + ")"), null, 2);
navigator.clipboard.writeText(jsonFormat)
.then(() => {
console.log("JSON object copied to clipboard.");
showNotification('Object successfully converted and copied to clipboard.', '#5E98C0');
});
} catch (error) {
showNotification('Clipboard does not contain a valid JSON object.', '#C20831');
}
});
function showNotification(message, color) {
const notification = document.createElement('div');
notification.textContent = message;
notification.style.cssText =
'position: fixed;\
top: 50%;\
left: 50%;\
transform: translate(-50%, -50%);\
background: ' + color + ';\
color: #fff;\
border-radius: 5px;\
padding: 10px;\
font-family: Arial;\
font-size: 16px;\
text-align: center;\
z-index: 9999;\
transition: opacity 0.5s ease-in-out;';
document.body.appendChild(notification);
setTimeout(() => {
notification.style.opacity = '1';
}, 100);
setTimeout(() => {
notification.style.opacity = '0';
setTimeout(() => {
document.body.removeChild(notification);
}, 500);
}, 2000);
}
`,
});
});