// 1. Enable interception
await runner.enableDownloads();
// 2. Trigger the download (e.g., clicking an export button)
const exportBtn = document.querySelector("#export-csv");
exportBtn.click();
// 3. Wait for the download to finish and get the URLs
const { local_url, public_url, mimeType, size } = await runner.waitFor(async () => {
const downloads = await runner.getDownloads();
const file = downloads.files[0];
if (file && file.status === 'done') {
return file;
} else if (file && file.status === 'failed') {
throw new Error(`Download failed: ${file.error}`);
}
});
console.log(`File: ${mimeType}, Size: ${size} bytes`);
// 4. Use local_url with runner.fetch() for high-performance direct access
// runner.fetch() uses a stealthy background proxy to bypass CORS and CSP.
const res = await runner.fetch(local_url);
const textData = await res.text();
console.log("File content length:", textData.length);
// 5. You can now publish the public_url for external use!
// The public_url is accessible via the Relay Server.
await runner.publishItems([{
id: 'my-file',
data: { downloadLink: public_url }
}]);
// 6. (Optional) Clear the queue if you are downloading multiple files sequentially
await runner.clearDownloads();