-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
129 lines (100 loc) · 4.04 KB
/
scripts.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
(function() {
var Ext = {
elements: {
saveBtn : document.getElementById('save'),
stopWordsArea : document.getElementById('stop-wordsarea'),
safeWordsArea : document.getElementById('safe-wordsarea'),
langs : document.getElementById('languages'),
replacers : document.getElementsByName('replacer'),
isactive : document.getElementById('isactive')
},
settings: null,
setActiveByDefault: function() {
var activate = this.elements.isactive;
activate.checked = (this.settings.active == true) ? true : false;
},
_uncheckAllReplacers: function() {
var replacers = this.elements.replacers;
for (var i = 0, len = replacers.length; i < len; i++) {
replacers[i].checked = false;
}
},
_getCheckedReplacer: function() {
var replacers = this.elements.replacers;
for (var i = replacers.length - 1; i >= 0; i--) {
if (replacers[i].checked) return replacers[i];
};
return null;
},
setReplacerByDefault: function() {
var field = document.getElementById('val' + this.settings.replacer);
this._uncheckAllReplacers();
if (field) {
field.checked = true;
} else {
this.elements.replacers[0].checked = true;
}
},
validateTextArea: function(text) {
var text = text.replace(/\s*[\r\n]+\s*/g, '\n').replace(/^\s+|\s+$/g, '');
return text.length ? text.split('\n') : [];
},
updateLocalData: function() {
var elements = this.elements;
var settings = this.settings;
var checkedReplacer = this._getCheckedReplacer();
var exports = {
userWords: this.validateTextArea(elements.stopWordsArea.value),
safeWords: this.validateTextArea(elements.safeWordsArea.value),
language: elements.langs.options[elements.langs.selectedIndex].value,
active: document.getElementsByName('isactive')[0].checked,
replacer: checkedReplacer.value,
customReplacer: document.getElementsByName('custom')[0].value
};
for (var prop in exports) {
if (settings.hasOwnProperty(prop)) {
settings[prop] = exports[prop];
}
}
this.sendData(settings);
},
sendData: function(settings) {
var settings = settings || this.settings;
chrome.storage.local.set({
'settings': settings
}, function(response) {
window.close()
});
chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
type: 'update'
});
});
},
init: function() {
var settings = this.settings;
var elements = this.elements;
this.setReplacerByDefault();
this.setActiveByDefault();
elements.saveBtn.addEventListener('click', this.updateLocalData.bind(this), false);
elements.stopWordsArea.value = settings.userWords.join('\n');
elements.safeWordsArea.value = settings.safeWords.join('\n');
for (var i = 0, len = elements.langs.options.length; i < len; i++) {
if (elements.langs.options[i].value == settings.language) {
elements.langs.options[i].setAttribute('selected', 'selected');
}
}
},
getSettings: function() {
var me = this;
chrome.storage.local.get('settings', function(response) {
me.settings = response.settings;
me.init();
});
}
};
Ext.getSettings();
})();