-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
164 lines (146 loc) · 4.09 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var x = function() {
var a = document.createElement('a');
a.className = 'delete';
a.tabIndex = -1;
var strong = document.createElement('strong');
strong.style.fontSize = '24px';
strong.innerHTML = '×';
a.appendChild(strong);
return a;
};
var input = function(className, value) {
var div = document.createElement('div');
div.className = 'input-field col s12';
var input = document.createElement('input');
input.type = 'text';
input.className = className;
input.value = value || "";
if (value !== undefined) {
input.className += ' valid';
}
if (className === 'find') {
input.autofocus = true;
}
div.appendChild(input);
return div;
};
var check = function(color) {
var div = document.createElement('div');
div.className = 'input-field col s12';
var input = document.createElement('input');
input.type = 'text';
input.className = 'color';
input.placeholder = '#hex color';
input.value = color || "";
if (color !== undefined) {
input.className += ' valid';
}
div.appendChild(input);
return div;
};
var row = function(values = '') {
var tr = document.createElement('tr');
var deleteButton = document.createElement('td');
deleteButton.appendChild(x());
var inputFind = document.createElement('td');
inputFind.appendChild(input('find', values.find || undefined));
var inputRepl = document.createElement('td');
inputRepl.appendChild(input('repl', values.repl || undefined));
var checkBox = document.createElement('td');
checkBox.appendChild(check(values.color || undefined));
tr.appendChild(deleteButton);
tr.appendChild(inputFind);
tr.appendChild(inputRepl);
tr.appendChild(checkBox);
return tr;
};
if (chrome.storage !== undefined) {
var words = chrome.storage.sync.get('words', function(data) {
if (data.words !== undefined) {
data.words.forEach(function(wordPair) {
$('tbody').append(row({
find: wordPair[0],
repl: wordPair[1],
color: wordPair[2]
}));
});
}
$('tbody').append(row());
return data.words;
});
}
else {
$('tbody').append(row());
}
$('body').on('input', 'input', function() {
if ($('input.find').last().val() !== "") {
var tr = row();
$('tbody').append(tr);
}
});
$('body').on('focus', 'input', function() {
var remove = false;
for (var i = $('input.find').length - 1; i >= 0; --i) {
if ($('input.find').eq(i).val() === "") {
if ($('input.repl').eq(i).val() === "") {
if ($(this).parents().eq(2)[0] === $('tr').last()[0] ||
(!$('input.find').eq(i).is(':focus') && !$('input.repl').eq(i).is(':focus'))) {
if (!remove)
remove = true;
else
$('input.find').parent().eq(i).parents()[1].remove();
}
}
}
}
if ($(this).val() !== "") {
$(this).parents().eq(2).after(row());
}
});
var timeout;
$('form').submit(function(event) {
event.preventDefault();
$('i').show();
clearTimeout(timeout);
timeout = setTimeout(function() {
$('i').hide();
}, 1000);
var words = Array();
for (var i = 0; i < $('input.find').length; i++) {
if ($('input.find').eq(i).val() !== "" && $('input.repl').eq(i).val() !== "") {
var tmpColor = $('input.color').eq(i).val().replace(/ /g, '').split(',');
if (tmpColor[0] === "")
tmpColor = undefined;
words.push([
$('input.find').eq(i).val(),
$('input.repl').eq(i).val(),
tmpColor
]);
}
}
if (chrome.storage !== undefined) {
chrome.storage.sync.set({'words': words});
}
if (chrome.tabs !== undefined) {
chrome.tabs.query({active: true, currentWindow: true}, function (arrayOfTabs) {
chrome.tabs.reload(arrayOfTabs[0].id);
});
}
$('input').filter(function() {
return this.value;
}).addClass('valid');
$('input.find:last').focus();
return false;
});
$('body').on('click', 'a.delete', function(e) {
if ($('a.delete').length === 1) {
$('input').val("");
}
else {
$(this).parents().eq(1).remove();
}
if ($('input.find').last().val() !== "") {
$('tbody').append(row());
}
$('input.find:last').focus();
});