forked from vcavallo/ReadToRelay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
73 lines (65 loc) · 1.97 KB
/
background.js
File metadata and controls
73 lines (65 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
chrome.action.onClicked.addListener(async (tab) => {
try {
// Extract article content from current tab
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['readability.js']
});
const results = await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => {
try {
const docClone = document.cloneNode(true);
const article = new Readability(docClone).parse();
return {
title: article?.title || document.title || 'Untitled',
content: article?.content || '',
textContent: article?.textContent || '',
excerpt: article?.excerpt || '',
byline: article?.byline || '',
url: window.location.href,
success: !!article
};
} catch (error) {
console.error('Readability extraction failed:', error);
return {
title: document.title || 'Untitled',
content: '',
textContent: '',
excerpt: '',
byline: '',
url: window.location.href,
success: false,
error: error.message
};
}
}
});
const articleData = results[0].result;
// Store article data temporarily
await chrome.storage.local.set({
currentArticle: articleData,
extractedAt: Date.now()
});
// Open reader page
chrome.tabs.create({
url: chrome.runtime.getURL('reader.html')
});
} catch (error) {
console.error('Failed to extract article:', error);
// Still open reader page, it can show the error
await chrome.storage.local.set({
currentArticle: {
title: 'Error',
content: '',
url: tab.url,
success: false,
error: 'Failed to extract article content'
},
extractedAt: Date.now()
});
chrome.tabs.create({
url: chrome.runtime.getURL('reader.html')
});
}
});