Parsing Large S3 XML Listings in MV3 with Offscreen
Last updated:
MV3 service workers do not have DOMParser. The recommended pattern is to spin up an offscreen document that runs in a DOM
context and communicates with the worker via chrome.runtime.sendMessage. This enables fast and robust XML parsing for S3/R2 listings.
Create offscreen document
// utils/offscreen-xml.js (excerpt)
await chrome.offscreen.createDocument({
url: chrome.runtime.getURL('offscreen.html'),
reasons: ['DOM_PARSER'],
justification: 'Parse XML responses from S3 API'
});
Parse XML with DOMParser
// offscreen.js (excerpt)
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'PARSE_XML') {
const doc = new DOMParser().parseFromString(message.payload.xmlText, 'application/xml');
const contents = Array.from(doc.querySelectorAll('Contents')).map(n => ({
key: n.querySelector('Key')?.textContent || '',
size: Number(n.querySelector('Size')?.textContent || '0'),
}));
sendResponse({ success: true, data: { items: contents } });
return true;
}
});
Fallbacks
When offscreen creation fails (e.g., limited environments), fall back to a regex parser with clear warnings.
