forked from yfuruyama/gcp-console-colorize
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoptions.js
98 lines (85 loc) · 2.47 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
function addCondition(condition) {
var $table = $('#conditions');
var $condition = $('<tr class="condition">');
var $pattern = $('<td><input type="text" placeholder="pattern" class="pattern"></input></td>');
var $color = $('<td><input type="text" class="color"></input></td>');
var $remove = $('<td><a href="#" id="remove">remove</a></td>');
$condition.append($pattern);
$condition.append($color);
$condition.append($remove);
var colorRgb = 'rgb(32, 100, 227)'; // initial color
if (condition) {
$pattern.find('.pattern')[0].value = condition.pattern;
var colorRgb = 'rgb(' + condition.color.r + ', '
+ condition.color.g + ', '
+ condition.color.b + ')';
$color.find('.color')[0].style.backgroundColor = colorRgb;
$color.find('.color')[0].value = colorRgb;
}
$condition.append($remove);
$table.append($condition);
// set event handler
$remove.click(function(event) {
$condition.remove();
});
$('.color').colorPicker({
opacity: false,
dark: '#fff',
light: '#fff',
});
}
function addEmptyCondition() {
addCondition(undefined);
}
function saveConditions() {
var conditions = [];
$('.condition').each(function(i, elem) {
var pattern = $(elem).find('.pattern')[0].value;
var color = $(elem).find('.color')[0].style.backgroundColor;
var colorParts = color.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/)
conditions.push({
pattern: pattern,
color: {
r: parseInt(colorParts[1]),
g: parseInt(colorParts[2]),
b: parseInt(colorParts[3]),
},
});
});
var setting = {
conditions: conditions
};
console.log("save setting: ", setting);
chrome.storage.sync.set(setting, function() {
console.log('saved!');
var $container = $('#container');
var $message = $('<span>saved!</span>');
$container.append($message);
setTimeout(function() {
$message.remove();
}, 1000);
});
}
(function() {
// initialize
$('#save').click(function(event) {
saveConditions();
});
$('#add').click(function(event) {
addEmptyCondition();
});
var defaultSetting = {
conditions: []
};
chrome.storage.sync.get(defaultSetting, function(setting) {
console.log("get setting: ", setting);
var conditions = setting.conditions;
if (conditions.length === 0) {
addEmptyCondition();
return;
}
conditions.forEach(function(condition) {
addCondition(condition);
});
});
}());