Sortana/details.js

64 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

document.addEventListener('DOMContentLoaded', async () => {
const logger = (await import(browser.runtime.getURL('logger.js'))).aiLog;
const midParam = new URLSearchParams(location.search).get('mid');
const messageId = parseInt(midParam, 10);
if (!messageId) {
logger('no ?mid → trying displayedMessage fallback');
const openerTabId = (await browser.tabs.getCurrent()).openerTabId;
const header = await browser.messageDisplay.getDisplayedMessage(openerTabId);
if (!header) {
logger('still no message aborting');
return;
}
loadMessage(header.id);
return;
}
loadMessage(messageId);
});
async function loadMessage(id) {
const storage = (globalThis.messenger ?? browser).storage;
const logMod = await import(browser.runtime.getURL('logger.js'));
const { debugLogging } = await storage.local.get('debugLogging');
logMod.setDebug(debugLogging === true);
const log = logMod.aiLog;
log('details page loaded', { debug: true });
try {
log('requesting message details', {}, id);
const { subject, results } = await browser.runtime.sendMessage({ type: 'sortana:getDetails', id });
log('received details', { debug: true }, { subject, results });
document.getElementById('subject').textContent = subject;
const container = document.getElementById('rules');
for (const r of results) {
log('rendering rule result', { debug: true }, r);
const article = document.createElement('article');
const color = r.matched === true ? 'is-success' : 'is-danger';
article.className = `message ${color} mb-4`;
const header = document.createElement('div');
header.className = 'message-header';
header.innerHTML = `<p>${r.criterion}</p>`;
const body = document.createElement('div');
body.className = 'message-body';
const status = document.createElement('p');
status.textContent = r.matched ? 'Matched' : 'Did not match';
const pre = document.createElement('pre');
pre.textContent = r.reason || '';
body.appendChild(status);
body.appendChild(pre);
article.appendChild(header);
article.appendChild(body);
container.appendChild(article);
}
document.getElementById('clear').addEventListener('click', async () => {
log('clearing cache for message', {}, id);
await browser.runtime.sendMessage({ type: 'sortana:clearCacheForMessage', id });
window.close();
});
} catch (e) {
log('failed to load details', { level: 'error' }, e);
}
}