-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
55 lines (47 loc) · 1.48 KB
/
background.js
File metadata and controls
55 lines (47 loc) · 1.48 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
chrome.sidePanel
.setPanelBehavior({ openPanelOnActionClick: true })
.catch((error) => console.error(error));
chrome.tabs.onActivated.addListener((activeInfo) => {
showSummary(activeInfo.tabId);
});
chrome.tabs.onUpdated.addListener(async (tabId) => {
showSummary(tabId);
});
async function showSummary(tabId) {
const tab = await chrome.tabs.get(tabId);
if (!tab.url.startsWith("http")) {
return;
}
const injection = await chrome.scripting.executeScript({
target: { tabId },
files: ["scripts/extract-content.js"],
});
chrome.storage.session.set({ pageContent: injection[0].result });
}
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "explainCode",
title: "Explain Code",
contexts: ["selection"],
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "explainCode") {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: logSelectedText,
});
}
});
async function logSelectedText() {
const selectedText = window.getSelection().toString();
const session = await ai.languageModel.create();
// Prompt the model and wait for the whole result to come back.
const result = await session.prompt(`Explain this code - ${selectedText}`);
console.log(result);
// Prompt the model and stream the result:
const stream = await session.promptStreaming(`Explain this code - ${selectedText}`);
for await (const chunk of stream) {
console.log(chunk);
}
}