-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
78 lines (65 loc) · 1.84 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
chrome.runtime.onMessage.addListener( data => {
if ( data.type === 'notification' ) {
tldr( data.message );
}
});
chrome.runtime.onInstalled.addListener( () => {
chrome.contextMenus.create({
id: 'tldr',
title: "TLDR: \"%s\"",
contexts:[ "selection" ]
});
});
chrome.contextMenus.onClicked.addListener( ( info, tab ) => {
if ( 'tldr' === info.menuItemId ) {
const result = getTLDR( info.selectionText );
// FIXME: pass the selected text to popup
// TODO: get the selected text and pass it to the input field in the popup and then inform the user to click on the icon and then regular behavior
alert(result);
}
} );
const tldr = message => {
chrome.storage.local.get( ['tldrCount'], data => {
let value = data.tldrCount || 0;
chrome.storage.local.set({ 'tldrCount': Number( value ) + 1 });
} );
return chrome.notifications.create(
'',
{
type: 'basic',
title: 'TLDR;',
message: message || 'TLDR;',
iconUrl: './assets/icons/128.png',
}
);
};
const getTLDR = message => {
const payload = {
model: "text-davinci-003",
prompt: "Give me a TLDR of the following text: " + message ,
temperature: 0.7,
max_tokens: 200,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
stream: false,
n: 1
};
<<<<<<< HEAD
const apiKey = "YOUR_API_KEY";
=======
// const apiKey = "your_api_key";
>>>>>>> e3f01680ef8b80231470eaaccdd8163636d3ff5f
fetch('https://api.openai.com/v1/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify(payload)
}).then(response => response.json()).then(data => {
console.log(data);
const summary = data.choices[0].text;
return summary;
});
}