-
Notifications
You must be signed in to change notification settings - Fork 37
/
options.js
67 lines (55 loc) · 1.72 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
;(function ($) {
'use strict';
function saveOptionsToChromeStorage(options, callback) {
chrome.storage.sync.set(options, callback);
}
function loadItemsFromChromeStorage(options, callback) {
chrome.storage.sync.get(options, function (items) {
callback(items);
});
}
function setFormValues(options) {
for (var key in options) {
var $formInput = $('#' + key);
if ($formInput.prop('type') === 'checkbox') {
$formInput.prop('checked', options[key]);
} else {
$formInput.prop('value', options[key]);
}
}
}
function getChangedValuesObject(changedAttributes) {
var updates = {};
for (var key in changedAttributes) {
updates[key] = changedAttributes[key].newValue;
}
return updates;
}
$(document).ready(function () {
loadItemsFromChromeStorage({
url: '',
tabSwitchingEnabled: false
// Add new items here to get them loaded and their values put in the form.
}, setFormValues);
$('#options-form').submit(function (e) {
e.preventDefault();
var $form = $(e.currentTarget);
var url = $form.find('#url').val();
var tabSwitchingEnabled = $form.find('#tabSwitchingEnabled').prop('checked');
saveOptionsToChromeStorage({
'url': url,
'tabSwitchingEnabled': tabSwitchingEnabled,
}, function () {
var status = $('#status');
status.text('Options saved.');
setTimeout(function () {
status.text('');
}, 3000);
});
});
chrome.storage.onChanged.addListener(function (changes) {
// The options were updated, update them here too.
setFormValues(getChangedValuesObject(changes));
});
});
} (this.$));