Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4494b68

Browse files
fix: resolve PR review issues - inject platform.js, remove duplicate, improve cleanup
- Add lib/platform.js to popup.js injection list (fixes missing rewriteImagePaths) - Remove duplicate rewriteImagePaths() from content-script.js (use platform.js version) - Improve listener cleanup in background.js with clearTimeout Co-authored-by: Samarth Gupta <[email protected]>
1 parent 14e1bb9 commit 4494b68

File tree

3 files changed

+7
-18
lines changed

3 files changed

+7
-18
lines changed

background.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,12 @@ function downloadImage(imageUrl, savePath) {
100100
}
101101

102102
// Listen for download completion to get the actual file path
103+
let timeoutId = null;
103104
const listener = (delta) => {
104105
if (delta.id !== downloadId) return;
105106

106107
if (delta.state && delta.state.current === 'complete') {
108+
clearTimeout(timeoutId);
107109
chrome.downloads.onChanged.removeListener(listener);
108110
// Get the actual file path
109111
chrome.downloads.search({ id: downloadId }, (results) => {
@@ -118,6 +120,7 @@ function downloadImage(imageUrl, savePath) {
118120
}
119121
});
120122
} else if (delta.state && delta.state.current === 'interrupted') {
123+
clearTimeout(timeoutId);
121124
chrome.downloads.onChanged.removeListener(listener);
122125
resolve({ success: false, originalUrl: imageUrl });
123126
}
@@ -126,7 +129,7 @@ function downloadImage(imageUrl, savePath) {
126129
chrome.downloads.onChanged.addListener(listener);
127130

128131
// Timeout after 30 seconds
129-
setTimeout(() => {
132+
timeoutId = setTimeout(() => {
130133
chrome.downloads.onChanged.removeListener(listener);
131134
resolve({ success: false, originalUrl: imageUrl, error: 'timeout' });
132135
}, 30000);

content-script.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
1-
// This script is injected after the lib scripts (readability, turndown, ezycopy)
2-
// extractContent(), extractContentWithImages(), and generateFilename() are available from lib/ezycopy.js
3-
4-
/**
5-
* Rewrite image URLs in markdown with local file paths
6-
*/
7-
function rewriteImagePaths(markdown, urlToPathMap) {
8-
let result = markdown;
9-
10-
for (const [originalUrl, localPath] of Object.entries(urlToPathMap)) {
11-
const escapedUrl = originalUrl.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
12-
const regex = new RegExp(`(!\\[[^\\]]*\\]\\()${escapedUrl}((?:\\s+"[^"]*")?\\))`, 'g');
13-
result = result.replace(regex, `$1${localPath}$2`);
14-
}
15-
16-
return result;
17-
}
1+
// This script is injected after the lib scripts (readability, turndown, ezycopy, platform)
2+
// extractContent(), extractContentWithImages(), generateFilename(), and rewriteImagePaths() are available from lib/
183

194
/**
205
* Build success message based on actions performed

popup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ document.addEventListener("DOMContentLoaded", async function () {
9696
"lib/turndown.js",
9797
"lib/turndown-plugin-gfm.js",
9898
"lib/ezycopy.js",
99+
"lib/platform.js",
99100
"content-script.js",
100101
],
101102
});

0 commit comments

Comments
 (0)