fix crawling + correct date/time format

This commit is contained in:
2026-10-07 16:01:15 +02:00
parent 9d8c2a0488
commit 25f323ff49
10 changed files with 183 additions and 65 deletions
+2 -2
View File
@@ -14,8 +14,8 @@
<body>
<h3 style="margin-top:0">Abacus → Kimai</h3>
<div class="row">
<label>From <input type="date" id="from"></label>
<label>To <input type="date" id="to"></label>
<label>From <input data-kind="date" id="from"></label>
<label>To <input data-kind="date" id="to"></label>
</div>
<button id="crawl" class="primary">Read days from MyAbacus</button>
<button id="stop" class="hidden">Stop</button>
+25 -9
View File
@@ -45,7 +45,7 @@ async function extract() {
return;
}
const total = merged.reduce((a, d) => a + d.minutes, 0);
$status.textContent = `Found ${merged.length} day(s), ${U.fmtDur(total)} total\n${merged[0].date} → ${merged[merged.length - 1].date}`;
$status.textContent = `Found ${merged.length} day(s), ${U.fmtDur(total)} total\n${U.fmtDate(merged[0].date)} → ${U.fmtDate(merged[merged.length - 1].date)}`;
browser.runtime.openOptionsPage();
}
@@ -63,19 +63,34 @@ function showCrawl(c) {
}
async function crawl() {
const from = $from.value, to = $to.value;
// Must be the first await (still inside the click gesture). Needed for MyAbacus iframes:
// activeTab alone only covers the top frame, and MV3 host permissions aren't always granted at install.
const ALL = { origins: ['<all_urls>'] };
if (!(await browser.permissions.contains(ALL))) {
$status.textContent = 'Firefox will ask for site access (needed to read MyAbacus iframes). Then click again.';
if (await browser.permissions.request(ALL)) $status.textContent = 'Access granted: click “Read days” again.';
return;
}
const granted = true;
const from = U.getField($from), to = U.getField($to);
if (!from || !to || from > to) throw new Error('Pick a valid date range');
await browser.storage.local.set({ crawlRange: { from, to } });
const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
const target = { tabId: tab.id, allFrames: true };
await browser.scripting.executeScript({ target, files: ['lib/util.js', 'lib/parse.js', 'lib/abacus-crawl.js'] });
$status.textContent = 'Looking for the card…';
await browser.storage.local.set({ crawlRequest: { from, to } });
// One single call: Firefox doesn't reliably share globals between separate executeScript calls
const results = await browser.scripting.executeScript({
target,
func: (f, t) => AbacusCrawl.run(f, t),
args: [from, to],
files: ['/lib/util.js', '/lib/parse.js', '/lib/abacus-crawl.js', '/lib/abacus-crawl-start.js'],
});
const res = results.map((r) => r.result).filter(Boolean);
if (res.every((r) => r.skipped)) throw new Error('No "Gestion des temps" card found on this tab');
const res = results.map((r) => r.result || { skipped: true, url: `frame ${r.frameId}`, injectError: r.error?.message || String(r.error || 'no result') });
if (res.every((r) => r.skipped)) {
const frames = res.map((r) => r.injectError
? `• ${r.url}: injection failed (${r.injectError})`
: `• ${r.url}: ${r.shadowRoots} shadow roots, angle-right icon: ${r.angleRight ? 'yes' : 'no'}, title: ${r.title ? 'yes' : 'no'}`);
throw new Error(`No "Gestion des temps" card found.${granted ? '' : ' (site access was refused: iframes can\'t be read)'}\n${frames.join('\n')}`);
}
const err = res.find((r) => r.error);
if (!err) browser.runtime.openOptionsPage();
}
@@ -91,8 +106,9 @@ browser.storage.onChanged.addListener((ch) => { if (ch.crawl) showCrawl(ch.crawl
(async () => {
const { crawlRange, crawl: last } = await browser.storage.local.get(['crawlRange', 'crawl']);
const today = U.isoDate(new Date());
$from.value = crawlRange?.from || U.monday(today);
$to.value = crawlRange?.to || today;
U.setupFields();
U.setField($from, crawlRange?.from || U.monday(today));
U.setField($to, crawlRange?.to || today);
// Only show a stale "running" if it's recent (page may have been reloaded mid-crawl)
if (last && (last.status !== 'running' || Date.now() - new Date(last.at) < 30000)) showCrawl(last);
})();