-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
128 lines (106 loc) · 3.7 KB
/
options.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* eslint-disable no-plusplus */
/* eslint-disable no-continue */
/* global chrome document */
/*! s
* Saves options to chrome.storage
* @param {Array} Serialized form data
* @return {}
*/
function saveOptions(data) {
const tabPref = data.find(element => element.name === 'tab-option');
chrome.storage.sync.set({
tabOption: tabPref.value,
// entities:
}, () => {
// Update status to let user know options were saved.
const status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(() => {
status.textContent = '';
}, 750);
});
}
/*!
* Restores select box and checkbox state using the preferences
* stored in chrome.storage.
* Might need to add checks for existense of objects
*/
function restoreOptions() {
chrome.storage.sync.get({
tabOption: 'new-tab', // this is a default in case there is no value in storage.
}, (items) => {
document.getElementById(items.tabOption).checked = true;
});
}
/*!
* Serialize all form data into an array
* (c) 2018 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Node} form The form to serialize
* @return {String} The serialized form data
*/
function serializeArray(form) {
const serialized = [];
for (let i = 0; i < form.elements.length; i++) {
const field = form.elements[i];
if (!field.name || field.disabled || field.type === 'file'
|| field.type === 'reset' || field.type === 'submit'
|| field.type === 'button') continue;
if (field.type === 'select-multiple') {
for (let n = 0; n < field.options.length; n++) {
if (!field.options[n].selected) continue;
serialized.push({
name: field.name,
value: field.options[n].value,
});
}
} else if (field.id.includes('-label-input')) {
// create key named with label field.value;
serialized.push({
name: {},
});
} else if (field.id.includes('-url-input')) {
// add value to previously added label value key
serialized[field.name] = field.value;
} else if ((field.type !== 'checkbox' && field.type !== 'radio')
|| field.checked) {
serialized.push({
name: field.name,
value: field.value,
});
}
}
return serialized;
}
// Debugging:
// chrome.storage.onChanged.addListener(function(changes, namespace) {
// for (key in changes) {
// var storageChange = changes[key];
// console.log('Storage key "%s" in namespace "%s" changed. ' +
// 'Old value was "%s", new value is "%s".',
// key,
// namespace,
// storageChange.oldValue,
// storageChange.newValue);
// }
// });
function urlComponent(name) {
return `<div id="${name}-section" >`
+ `<label for="${name}-label-input" class="display-block" >Label for ${name}:</label>`
+ `<input id="${name}-label-input" name="${name}"`
+ 'class="border border-radius width-small height-small" type="text">'
+ `<label for="${name}-url-input" class="display-block" >URL for ${name}:</label>`
+ `<input id="${name}-url-input" name="${name}"`
+ 'class="border border-radius width-small height-small" type="text">'
+ '</div>';
}
document.addEventListener('DOMContentLoaded', () => {
restoreOptions();
const entitySection = document.querySelector('#entity-section');
entitySection.innerHTML += urlComponent('Example');
document.addEventListener('click', (event) => {
if (!event.target.matches('#save-button')) return;
event.preventDefault();
const data = serializeArray(document.querySelector('#form-options'));
saveOptions(data);
});
});