Skip to content

Commit 1380644

Browse files
committed
Fix metadata.json download and handle missing duration property
- Replace URL.createObjectURL with data URL in metadata writer (blob URLs not available in Manifest V3 service workers) - Add fallback for missing audio-duration property in chapter extraction - Handle cases where BIF structure may not include duration metadata
1 parent cf9ad96 commit 1380644

2 files changed

Lines changed: 14 additions & 19 deletions

File tree

chrome-extension/background/metadata-writer.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,18 @@ export class MetadataWriter {
1717
console.log('[Metadata Writer] Saving metadata file');
1818

1919
const metadataContent = JSON.stringify({ metadata, chapters }, null, 2);
20-
const blob = new Blob([metadataContent], { type: 'application/json' });
21-
const url = URL.createObjectURL(blob);
2220

23-
try {
24-
const downloadId = await chrome.downloads.download({
25-
url: url,
26-
filename: `libby-downloads/${sanitizeFilename(bookTitle)}/metadata.json`,
27-
saveAs: false,
28-
});
21+
// Use data URL instead of blob URL (blob URLs don't work in service workers)
22+
const dataUrl = 'data:application/json;charset=utf-8,' + encodeURIComponent(metadataContent);
2923

30-
console.log('[Metadata Writer] Metadata file download started');
24+
const downloadId = await chrome.downloads.download({
25+
url: dataUrl,
26+
filename: `libby-downloads/${sanitizeFilename(bookTitle)}/metadata.json`,
27+
saveAs: false,
28+
});
3129

32-
// Revoke blob URL after 30 seconds
33-
setTimeout(() => URL.revokeObjectURL(url), 30000);
30+
console.log('[Metadata Writer] Metadata file download started');
3431

35-
return downloadId;
36-
} catch (error) {
37-
// Clean up blob URL on error
38-
URL.revokeObjectURL(url);
39-
throw error;
40-
}
32+
return downloadId;
4133
}
4234
}

chrome-extension/iframe/iframe-extractor.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,18 @@ async function extractBookData() {
102102
const url =
103103
location.origin + '/' + spine.meta.path + '?' + odreadCmptParams[spine.spinePosition];
104104

105+
// Try different possible duration properties (fallback to 0 if not available)
106+
const duration = spine.meta['audio-duration'] || spine.meta.duration || spine.duration || 0;
107+
105108
chapters.push({
106109
index: spine.meta['-odread-spine-position'],
107110
title: `Part ${spine.meta['-odread-spine-position'] + 1}`,
108111
url: url,
109-
duration: spine.meta['audio-duration'],
112+
duration: duration,
110113
startTime: cumulativeTime,
111114
});
112115

113-
cumulativeTime += spine.meta['audio-duration'];
116+
cumulativeTime += duration;
114117
}
115118

116119
// Get chapter titles from TOC

0 commit comments

Comments
 (0)