-
Notifications
You must be signed in to change notification settings - Fork 1
/
copy-settings-json.plugin.js
84 lines (70 loc) · 2.33 KB
/
copy-settings-json.plugin.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
/**
* Copy Settings plugin
* v.1.1, 2022-11-08
*
* Copy a json representation of the settings of this image to the clipboard
*
* By JeLuF
*
*/
(function() {
const ID_PREFIX="copy-settings";
const popupContainer = document.createElement('div');
popupContainer.id = `${ID_PREFIX}-popup`;
popupContainer.style.display = 'none';
popupContainer.style.position = 'absolute';
popupContainer.style.width = '20em';
popupContainer.style.top = '1em';
popupContainer.style.right = '1em';
popupContainer.style.padding = '0.5em';
popupContainer.style["border-radius"] = '0.5em';
document.body.appendChild(popupContainer);
function popup(msg,fg='var(--button-text-color)',bg='var(--button-color)') {
popupContainer.innerHTML = msg;
popupContainer.style.background = bg;
popupContainer.style.color = fg;
popupContainer.style.display = 'block';
setTimeout( function(){popupContainer.style.display='none';}, 1500 );
}
function fallbackCopyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text;
// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
popup('Copying text command was ' + msg);
} catch (err) {
console.error('Fallback: Could not copy text: ', err);
popup('Fallback: Oops, unable to copy');
}
document.body.removeChild(textArea);
}
function copyTextToClipboard(text) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(function() {
popup('Copied to clipboard');
}, function(err) {
console.error('Async: Could not copy text: ', err);
popup('Could not copy text');
});
}
PLUGINS['IMAGE_INFO_BUTTONS'].push({
text: 'Copy settings (JSON)',
on_click: function(origRequest, image) {
copyTextToClipboard(JSON.stringify(origRequest, undefined, 4));
},
filter: function(origRequest, image) {
return true;
}
})
})();