-
Notifications
You must be signed in to change notification settings - Fork 4
/
popup.js
126 lines (118 loc) · 4.3 KB
/
popup.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
document
.getElementById("summarizeBtn")
.addEventListener("click", async function () {
const selectedLanguage = document.getElementById("languageSelect").value;
const summaryElement = document.getElementById("summary");
summaryElement.innerText = "";
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
});
chrome.scripting.executeScript(
{
target: { tabId: tab.id },
func: () => window.getSelection().toString(),
},
async function (selection) {
const selectedText = selection[0].result;
if (selectedText) {
// Summarize selected text
chrome.runtime.sendMessage({
command: "summarizeSelectedText",
text: selectedText,
language: selectedLanguage,
});
} else if (!tab.url.endsWith(".pdf")) {
// Summarize whole page if no text is selected and it's not a PDF
chrome.runtime.sendMessage({
command: "summarize",
language: selectedLanguage,
});
} else {
chrome.scripting
.executeScript({
target: { tabId: tab.id },
func: (selectedLanguage) => {
localStorage.setItem("selectedLanguage", selectedLanguage); // Ensure it's set in the right context
// Now, inject the script or continue other operations that depend on this setting
},
args: [selectedLanguage],
})
.then(() => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["getContentScript.js"],
});
});
}
}
);
});
document
.getElementById("explainBtn")
.addEventListener("click", async function () {
const selectedLanguage = document.getElementById("languageSelect").value;
const summaryElement = document.getElementById("summary");
summaryElement.innerText = "";
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
});
chrome.scripting.executeScript(
{
target: { tabId: tab.id },
func: () => window.getSelection().toString(),
},
async function (selection) {
const selectedText = selection[0].result;
if (selectedText) {
chrome.runtime.sendMessage({
command: "explainSelectedText",
text: selectedText,
language: selectedLanguage,
});
} else if (!tab.url.endsWith(".pdf")) {
chrome.runtime.sendMessage({
command: "explain",
language: selectedLanguage,
});
} else {
chrome.scripting
.executeScript({
target: { tabId: tab.id },
func: (selectedLanguage) => {
localStorage.setItem("selectedLanguage", selectedLanguage); // Ensure it's set in the right context
// Now, inject the script or continue other operations that depend on this setting
},
args: [selectedLanguage],
})
.then(() => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["getContentScript.js"],
});
});
}
}
);
});
// In popup.js
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
const summaryElement = document.getElementById("summary");
if (message.action === "update") {
summaryElement.innerHTML += message.summary; // Append new content
} else if (message.action === "complete") {
const converter = new showdown.Converter(); // Initialize Showdown
summaryElement.innerHTML += "<p>---End of summary---"; // Indicate completion
summaryElement.innerHTML += "<p>made by https://github.com/eddieoz/lmstudio-webpage-summarizer"
summaryElement.innerHTML = converter.makeHtml(summaryElement.innerHTML);
}
});
document.getElementById("copyBtn").addEventListener("click", function () {
const summaryText = document.getElementById("summary").innerText;
navigator.clipboard.writeText(summaryText).then(function() {
console.log('Text copied to clipboard');
}).catch(function(err) {
console.error('Could not copy text: ', err);
});
});