-
Notifications
You must be signed in to change notification settings - Fork 8
/
popup.js
78 lines (69 loc) · 2.3 KB
/
popup.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
function populateText(formattedText) {
const textElem = document.getElementById('textToCopy');
textElem.value = formattedText;
textElem.focus();
textElem.select();
}
function populateFormatGroup(options) {
const defaultFormat = options['defaultFormat'];
let radios = [];
const cnt = getFormatCount(options);
let group = document.getElementById('formatGroup');
while (group.hasChildNodes()) {
group.removeChild(group.childNodes[0]);
}
for (let i = 1; i <= cnt; ++i) {
let radioId = 'format' + i;
let btn = document.createElement('input');
btn.setAttribute('type', 'radio');
btn.setAttribute('name', 'fomrat');
btn.setAttribute('id', radioId);
btn.setAttribute('value', i);
if (i == defaultFormat) {
btn.setAttribute('checked', 'checked');
}
btn.addEventListener('click', async e => {
const formatID = e.target.value;
const format = options['format' + formatID];
const asHTML = options['html' + formatID];
const formattedText = await copyLinkToClipboard(format, asHTML, options);
populateText(formattedText);
});
let optTitle = options['title' + i];
let text = document.createTextNode(optTitle);
let label = document.createElement('label');
label.appendChild(btn);
label.appendChild(text);
group.appendChild(label);
}
}
function getSelectedFormatID() {
for (let i = 1; i <= FORMAT_MAX_COUNT; ++i) {
let radio = document.getElementById('format' + i);
if (radio && radio.checked) {
return i;
}
}
return undefined;
}
async function init() {
document.getElementById('saveDefaultFormatButton').addEventListener('click', async () => {
let formatID = getSelectedFormatID();
if (formatID) {
await browser.runtime.sendMessage({
messageID: 'update-default-format',
formatID
});
}
});
document.getElementById('optionsButton').addEventListener('click', () => {
browser.runtime.openOptionsPage();
});
const options = await gettingOptions();
const format = options['format' + options.defaultFormat];
const asHTML = options['html' + options.defaultFormat];
let formattedText = await copyLinkToClipboard(format, asHTML, options);
populateText(formattedText);
populateFormatGroup(options);
}
document.addEventListener('DOMContentLoaded', init);