-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
174 lines (161 loc) · 7.05 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
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
165
166
167
168
169
170
171
172
173
174
document.addEventListener("DOMContentLoaded", restoreOptions);
function restoreOptions() {
getStorageValue('auto_targeting', _autoTargeting => {
if(typeof(_autoTargeting) === "undefined"){
setStorageValue('auto_targeting', true);
autoTargeting = true;
}
else {
setStorageValue('auto_targeting', _autoTargeting);
autoTargeting = _autoTargeting;
}
displayTargetingDivs();
document.querySelector("#use_auto_targeting").checked = autoTargeting;
});
getStorageValue('target_mode', _targetMode => {
setStorageValue('target_mode', _targetMode || "both");
targetMode = _targetMode || "both";
if(targetMode === 'both') {
document.querySelector("#use_celebrity_targets").checked = true;
document.querySelector("#use_custom_targets").checked = true;
}
else if(targetMode === 'celebrity'){
document.querySelector("#use_celebrity_targets").checked = true;
}
else if(targetMode === 'custom'){
document.querySelector("#use_custom_targets").checked = true;
}
});
getStorageValue('target_source', _targetSource => {
targetSource = _targetSource;
});
getStorageValue('target_name', _targetName => {
targetName = _targetName;
});
getStorageValue('custom_victims', victimsDict => {
victimsDict = victimsDict || {};
setStorageValue('custom_victims', victimsDict);
addSelectorOptions('custom_selector', 'custom', victimsDict);
});
importCelebritiesVictimsEmbedding().then(victimsDict => {
addSelectorOptions('celebrity_selector', 'celebrity', victimsDict);
});
}
let loading = false;
async function saveNewVictim(e) {
e.preventDefault();
if(!loading) {
loading = true;
document.querySelector("#target_save").innerHTML = 'Saving...';
document.querySelector("#target_adding_status").innerHTML = '';
try {
let name = document.querySelector("#target_name").value,
imageFiles = document.querySelector("#target_images").files;
if(name.length === 0){
document.querySelector("#target_adding_status").innerHTML = 'You should choose a name for your target.';
document.querySelector("#target_adding_status").style.color = 'red';
}
else if(imageFiles.length === 0){
document.querySelector("#target_adding_status").innerHTML = 'You should select at least one image.';
document.querySelector("#target_adding_status").style.color = 'red';
}
else {
await detectorInitializer();
await addTargetVictim(name, imageFiles);
addNewOptionToSelector("custom_selector", name);
document.querySelector("#target_name").value = '';
document.querySelector("#target_images").value = '';
document.querySelector("#target_adding_status").innerHTML = 'Target `' + name + '` added.';
document.querySelector("#target_adding_status").style.color = 'green';
}
}
catch (err) {
document.querySelector("#target_adding_status").innerHTML = 'An error just happened on adding a new target!';
document.querySelector("#target_adding_status").style.color = 'red';
}
loading = false;
document.querySelector("#target_save").innerHTML = 'Add';
}
}
document.querySelector("form#adding_target_form").addEventListener("submit", saveNewVictim);
// -----------
let autoTargeting, targetMode, targetSource, targetName;
document.querySelector("#use_auto_targeting").addEventListener("change", function () {
autoTargeting = this.checked;
displayTargetingDivs();
});
document.querySelectorAll("#use_celebrity_targets, #use_custom_targets").forEach(item => {
item.addEventListener("change", function () {
let celebCheck = document.querySelector("#use_celebrity_targets").checked,
customCheck = document.querySelector("#use_custom_targets").checked;
if(celebCheck && celebCheck) targetMode = 'both';
else if(celebCheck) targetMode = 'celebrity';
else if(customCheck) targetMode = 'custom';
else targetMode = undefined;
});
});
document.getElementById("celebrity_selector").addEventListener("change", function () {
targetSelected('celebrity', this.value);
});
document.getElementById("custom_selector").addEventListener("change", function () {
targetSelected('custom', this.value);
});
function displayTargetingDivs(){
if(autoTargeting){
document.querySelector("#auto_targeting_is_true").style.display = 'block';
document.querySelector("#auto_targeting_is_false").style.display = 'none';
}
else {
document.querySelector("#auto_targeting_is_true").style.display = 'none';
document.querySelector("#auto_targeting_is_false").style.display = 'block';
}
}
function addSelectorOptions(selectorId, selectorSource, dictionary){
let sel = document.getElementById(selectorId);
let opt;
for(let name in dictionary){
opt = document.createElement('option');
opt.appendChild(document.createTextNode(name.split('_').join(' ')));
opt.value = name;
if(targetSource === selectorSource && targetName === name) {
opt.setAttribute('selected', 'true');
document.getElementById('choosed_target_shower').innerHTML = name;
}
sel.appendChild(opt);
}
}
function addNewOptionToSelector(selectorId, name) {
let sel = document.getElementById(selectorId);
let opt = document.createElement('option');
opt.appendChild(document.createTextNode(name.split('_').join(' ')));
opt.value = name;
sel.appendChild(opt);
}
function targetSelected(_targetSource, _targetName) {
document.getElementById('choosed_target_shower').innerHTML = _targetName;
targetSource = _targetSource;
targetName = _targetName;
}
document.querySelector("form#choosing_mode_form").addEventListener("submit", (e) => {
e.preventDefault();
if(autoTargeting) {
if(typeof(targetMode) === 'undefined') {
document.querySelector("#choosing_target_status").innerHTML = 'You must choose at least one option.';
document.querySelector("#choosing_target_status").style.color = 'red';
return;
}
}
else {
if(typeof(targetSource) === 'undefined' || typeof(targetName) === 'undefined') {
document.querySelector("#choosing_target_status").innerHTML = 'Please select a victim.';
document.querySelector("#choosing_target_status").style.color = 'red';
return;
}
}
setStorageValue('auto_targeting', autoTargeting);
setStorageValue('target_mode', targetMode);
setStorageValue('target_source', targetSource);
setStorageValue('target_name', targetName);
document.querySelector("#choosing_target_status").innerHTML = 'Saved';
document.querySelector("#choosing_target_status").style.color = 'green';
});