-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
78 lines (64 loc) · 1.8 KB
/
background.js
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
74
75
76
77
78
import {getSummaryFromArticle} from './summarizer.js';
const page = {
content: null, summary: null, title: null,
}
const openOptionsPage = () => {
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
window.location.href = chrome.runtime.getURL('options.html')
}
}
const getOpenAIKey = () => {
return new Promise((resolve, reject) => {
chrome.storage.sync.get({
apiKey: '',
}, function (items) {
resolve(items.apiKey)
});
})
}
const getPromptQuestion = () => {
return new Promise((resolve, reject) => {
chrome.storage.sync.get({
promptQuestion: '',
}, function (items) {
resolve(items.promptQuestion)
});
})
}
const getMaxLengthToSummarize = () => {
return new Promise((resolve, reject) => {
chrome.storage.sync.get({
maxLength: '',
}, function (items) {
resolve(items.maxLength)
});
})
}
const getCurrentURL = async () => {
let queryOptions = {active: true, lastFocusedWindow: true};
let [tab] = await chrome.tabs.query(queryOptions);
if (tab && tab.url) {
return tab.url;
}
return null;
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'documentParsed') {
page.title = message.parsed.title;
page.content = message.parsed.content;
return false;
}
if (message.action === 'loadSummary') {
getSummaryFromArticle(page.content).then((summary) => {
page.summary = summary;
return sendResponse(page);
})
return true;
}
return false;
});
export {
openOptionsPage, getOpenAIKey, getPromptQuestion, getMaxLengthToSummarize, getCurrentURL
}