-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
272 lines (238 loc) · 8.66 KB
/
Copy pathcontent.js
File metadata and controls
272 lines (238 loc) · 8.66 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* global TeletalkCapture, TeletalkFill, TeletalkMatch */
(function () {
var STORAGE_KEY = 'profile';
var SETTINGS_KEY = 'settings';
var UI_KEY = 'uiState';
var forceVisible = false;
var lastUnmatched = [];
function toast(msg, ms) {
ms = ms || 3500;
var el = document.getElementById('tt-fill-toast');
if (!el) {
el = document.createElement('div');
el.id = 'tt-fill-toast';
document.body.appendChild(el);
}
el.textContent = msg;
el.classList.add('show');
clearTimeout(el._timer);
el._timer = setTimeout(function () { el.classList.remove('show'); }, ms);
}
function getProfile(cb) {
chrome.storage.local.get(STORAGE_KEY, function (data) {
cb(TeletalkMatch.sanitizeProfile(data[STORAGE_KEY] || {}));
});
}
function saveProfile(profile, cb) {
var obj = {};
obj[STORAGE_KEY] = profile;
chrome.storage.local.set(obj, cb);
}
function saveUiState(collapsed) {
var obj = {};
obj[UI_KEY] = { collapsed: collapsed };
chrome.storage.local.set(obj);
}
function showUnmatched(list) {
lastUnmatched = list || [];
var wrap = document.getElementById('tt-fill-unmatched');
if (!wrap) return;
if (!lastUnmatched.length) {
wrap.innerHTML = '';
wrap.style.display = 'none';
return;
}
var unique = lastUnmatched.filter(function (l, i, arr) { return arr.indexOf(l) === i; });
wrap.style.display = 'block';
wrap.innerHTML =
'<details><summary>' + unique.length + ' unmatched field(s)</summary><ul>' +
unique.slice(0, 30).map(function (l) { return '<li>' + l + '</li>'; }).join('') +
(unique.length > 30 ? '<li>…and ' + (unique.length - 30) + ' more</li>' : '') +
'</ul></details>';
}
function setBarVisible(show) {
var bar = document.getElementById('tt-fill-bar');
if (!bar) return;
if (show) bar.classList.remove('tt-hidden');
else bar.classList.add('tt-hidden');
}
function refreshVisibility() {
var hasForm = TeletalkMatch.hasApplicationForm();
setBarVisible(forceVisible || hasForm);
}
function updateStatus() {
var statusEl = document.getElementById('tt-fill-status');
var gapEl = document.getElementById('tt-fill-gap');
var fillBtn = document.getElementById('tt-fill-autofill');
if (!statusEl) return;
refreshVisibility();
getProfile(function (profile) {
var savedCount = TeletalkMatch.meaningfulProfileKeys(profile).length;
var preview = TeletalkFill.countMatchable(profile);
if (!savedCount) {
statusEl.className = 'tt-empty';
statusEl.innerHTML = 'No saved data yet.<br>Fill the form once, then click <strong>Save Form</strong>.';
if (gapEl) gapEl.textContent = '';
if (fillBtn) fillBtn.disabled = true;
showUnmatched([]);
return;
}
statusEl.className = '';
if (fillBtn) fillBtn.disabled = preview.matchable === 0 && preview.total > 0;
if (preview.total === 0) {
statusEl.innerHTML =
'<strong>' + savedCount + '</strong> field(s) saved.<br>Open an application form page to auto-fill.';
if (gapEl) gapEl.textContent = '';
} else {
statusEl.innerHTML =
'<strong>' + preview.matchable + '</strong> of <strong>' + preview.total + '</strong> fields can auto-fill' +
'<br><span style="color:#666;font-size:12px">' + savedCount + ' total saved in profile</span>';
if (gapEl) {
gapEl.textContent = preview.gap > 0
? preview.gap + ' field(s) on this page have no saved match'
: 'All visible fields have saved data';
}
}
});
}
function doSave(done) {
var captured = TeletalkCapture.capturePage();
if (!captured.count) {
toast('No filled fields found on this page.');
if (done) done({ count: 0 });
return;
}
getProfile(function (existing) {
var before = Object.keys(existing).length;
var merged = TeletalkMatch.sanitizeProfile(Object.assign({}, existing, captured.profile));
var added = Object.keys(merged).length - before;
saveProfile(merged, function () {
var msg = 'Saved ' + captured.count + ' field(s). Total: ' + Object.keys(merged).length;
if (before >= 20 && added < 3 && added >= 0) {
msg = 'Updated ' + Math.max(added, captured.count) + ' field(s). Total: ' + Object.keys(merged).length;
}
toast(msg);
forceVisible = true;
setBarVisible(true);
updateStatus();
if (done) done({ count: captured.count, total: Object.keys(merged).length });
});
});
}
function doFill(done) {
getProfile(function (profile) {
if (!TeletalkMatch.meaningfulProfileKeys(profile).length) {
toast('No saved data. Fill form once, then click Save Form.');
if (done) done({ filled: 0, total: 0, unmatched: [] });
return;
}
forceVisible = true;
setBarVisible(true);
TeletalkFill.fillPageWithRetry(profile, function (result) {
var msg = 'Filled ' + result.filled + '/' + result.total + ' fields';
toast(msg);
showUnmatched(result.unmatched);
updateStatus();
if (done) done(result);
});
});
}
function injectBar() {
if (document.getElementById('tt-fill-bar')) return;
var bar = document.createElement('div');
bar.id = 'tt-fill-bar';
bar.className = 'tt-hidden';
bar.innerHTML =
'<div class="tt-fill-header">' +
'<span class="tt-fill-title">Teletalk Form Fill</span>' +
'<button type="button" id="tt-fill-toggle" title="Minimize">−</button>' +
'</div>' +
'<div class="tt-fill-body">' +
'<div id="tt-fill-status">Loading…</div>' +
'<div id="tt-fill-gap"></div>' +
'<div id="tt-fill-unmatched" style="display:none"></div>' +
'<div class="tt-fill-actions">' +
'<button type="button" class="tt-btn" id="tt-fill-save">Save Form</button>' +
'<button type="button" class="tt-btn" id="tt-fill-autofill">Auto Fill</button>' +
'</div>' +
'</div>';
document.body.appendChild(bar);
document.getElementById('tt-fill-save').addEventListener('click', function (e) {
e.stopPropagation();
doSave();
});
document.getElementById('tt-fill-autofill').addEventListener('click', function (e) {
e.stopPropagation();
doFill();
});
document.getElementById('tt-fill-toggle').addEventListener('click', function (e) {
e.stopPropagation();
bar.classList.toggle('tt-collapsed');
var collapsed = bar.classList.contains('tt-collapsed');
this.textContent = collapsed ? '+' : '−';
saveUiState(collapsed);
});
bar.querySelector('.tt-fill-header').addEventListener('click', function (e) {
if (e.target.id === 'tt-fill-toggle') return;
if (bar.classList.contains('tt-collapsed')) {
bar.classList.remove('tt-collapsed');
document.getElementById('tt-fill-toggle').textContent = '−';
saveUiState(false);
}
});
chrome.storage.local.get(UI_KEY, function (data) {
if (data[UI_KEY] && data[UI_KEY].collapsed) {
bar.classList.add('tt-collapsed');
document.getElementById('tt-fill-toggle').textContent = '+';
}
});
}
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.action === 'save') {
forceVisible = true;
doSave(sendResponse);
return true;
}
if (msg.action === 'fill') {
forceVisible = true;
doFill(sendResponse);
return true;
}
if (msg.action === 'status') {
getProfile(function (profile) {
sendResponse(TeletalkFill.countMatchable(profile));
});
return true;
}
if (msg.action === 'showBar') {
forceVisible = true;
setBarVisible(true);
updateStatus();
sendResponse({ ok: true });
return false;
}
return false;
});
injectBar();
updateStatus();
chrome.storage.onChanged.addListener(function (changes, area) {
if (area === 'local' && (changes[STORAGE_KEY] || changes[UI_KEY])) updateStatus();
});
var statusTimer;
function scheduleStatusRefresh() {
clearTimeout(statusTimer);
statusTimer = setTimeout(updateStatus, 600);
}
window.addEventListener('load', scheduleStatusRefresh);
document.addEventListener('click', function (e) {
if (e.target.closest('button[type="submit"], input[type="submit"], .custom-form')) {
scheduleStatusRefresh();
}
});
chrome.storage.local.get(SETTINGS_KEY, function (data) {
var settings = data[SETTINGS_KEY] || {};
if (settings.autoFillOnLoad && TeletalkMatch.hasApplicationForm()) {
setTimeout(function () { doFill(); }, 800);
}
});
})();