-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
64 lines (59 loc) · 2.53 KB
/
background.js
File metadata and controls
64 lines (59 loc) · 2.53 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
// Background service worker for keyboard shortcut command.
chrome.commands.onCommand.addListener(async (command) => {
if (command === 'fill-form') {
await runFill();
}
});
async function runFill(existingTab) {
try {
const tab = existingTab || (await chrome.tabs.query({ active: true, currentWindow: true }))[0];
if (!tab?.id) return;
const { defaultValue = '' } = await chrome.storage.local.get('defaultValue');
await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: fillFields,
args: [defaultValue || '']
});
} catch (e) {
console.error('FormFully command failed', e);
}
}
function fillFields(inputValue) {
const allInputs = document.getElementsByTagName('input');
for (let i = 0; i < allInputs.length; i++) {
const inp = allInputs[i];
if (inp.type && inp.type.toLowerCase() === 'hidden') continue;
if (inp.type === 'date') inp.value = formatDate(new Date());
else if (['datetime', 'datetime-local'].includes(inp.type)) inp.value = formatDateTime(new Date());
else if (inp.type === 'month') inp.value = formatMonth(new Date());
else if (inp.type === 'week') inp.value = formatWeek(new Date());
else if (inp.type === 'time') inp.value = formatTime(new Date());
else if (inp.type === 'color') inp.value = randomColor();
else if ((inputValue || '').trim() === '') inp.value = Math.floor(Math.random() * 5) + 1;
else inp.value = inputValue;
}
try { document.getElementById('a_next')?.click(); } catch (e) { }
function formatDate(date) { return date.toISOString().split('T')[0]; }
function formatMonth(date) { return date.toISOString().split('T')[0].slice(0, 7); }
function formatDateTime(date) { return date.toISOString().slice(0, 16); }
function formatWeek(date) {
const year = date.getFullYear();
const weekNumber = getISOWeek(date);
return `${year}-W${weekNumber}`;
}
function getISOWeek(date) {
const tempDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
const dayNum = tempDate.getUTCDay() || 7;
tempDate.setUTCDate(tempDate.getUTCDate() + 4 - dayNum);
const yearStart = new Date(Date.UTC(tempDate.getUTCFullYear(), 0, 1));
return Math.ceil((((tempDate - yearStart) / 86400000) + 1) / 7);
}
function formatTime(date) {
const h = String(date.getHours()).padStart(2, '0');
const m = String(date.getMinutes()).padStart(2, '0');
return `${h}:${m}`;
}
function randomColor() {
return '#' + Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0');
}
}