-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
63 lines (58 loc) · 1.88 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
groupTabs();
async function groupTabs() {
try {
const tabsCollection = await getAllTabsUrl();
const tabsUrl = Object.keys(tabsCollection);
console.log(tabsCollection);
const apiKey = '';
const response = await fetch('https://api.openai.com/v1/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
"model": "text-davinci-003",
"prompt": `Please classify and group related urls ${tabsUrl}. Please response a stringified json format: name of the classification, array of following related urls. Example: {"AWS Related": ["https...", "https://"]}`,
"max_tokens": 3800,
})
});
const responseData = await response.json();
const responseText = JSON.parse(responseData.choices[0].text);
console.log(responseText);
for (let key in responseText) {
console.log(key);
console.log(typeof key);
let tabIds = [];
let urlList = responseText[key];
console.log(urlList);
for (let index in urlList) {
let tabId = tabsCollection[urlList[index]];
tabIds.push(tabId);
};
const groupId = await chrome.tabs.group({ tabIds });
console.log(groupId);
await chrome.tabGroups.update(groupId, { title: key });
}
window.close();
} catch (error) {
console.error(error);
}
}
/*
Define a function to get all current tabs in current window
*/
function getAllTabsUrl() {
// Create a new Promise
return new Promise((resolve, reject) => {
// Use chrome.tabs.query() to get all tabs
chrome.tabs.query({ currentWindow: true }, function (tabs) {
let tabsCollection = {};
tabs.forEach(function (tab) {
tabsCollection[tab.url] = tab.id;
});
// Resolve the Promise with the result
resolve(tabsCollection);
});
});
};