-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
141 lines (130 loc) · 4.95 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
let isSummary = false;
let summaryText = "";
document.addEventListener("DOMContentLoaded", () => {
const summarizeButton = document.getElementById("summarize");
const summaryContainer = document.getElementById("summary");
const tagContainer = document.getElementById("tags");
const numSentencesInput = document.getElementById("num-sentences");
const sentimentContainer = document.getElementById("sentiment-container");
const copyCitationButton = document.getElementById("copy-citation");
const copySummaryButton = document.getElementById("copy-summary");
const resultsContainer = document.getElementById("results");
const numSentences = document.getElementById('num-sentences');
const targetLang = document.getElementById("language-selection");
copyCitationButton.classList.add("hidden");
copySummaryButton.classList.add("hidden");
numSentences.addEventListener('input', function () {
if (parseInt(this.value) > 10) {
this.value = 10;
}
});
summarizeButton.addEventListener("click", async () => {
const numSentences = numSentencesInput.value;
const target_lang = targetLang.value;
const activeTab = await getActiveTab();
summaryContainer.innerText = "";
const loadingIcon = document.getElementById("loading-icon");
loadingIcon.style.display = "inline-block";
const result = await chrome.scripting.executeScript({
target: { tabId: activeTab.id },
func: summarize,
args: [numSentences, target_lang],
});
loadingIcon.style.display = "none";
resultsContainer.classList.remove("hidden");
if (result[0].result) {
summaryText = result[0].result["summary"] || "Summary not available";
summaryContainer.innerText = summaryText;
isSummary = true;
// Show the "Copy Citation" and "Copy Summary" buttons
copyCitationButton.classList.remove("hidden");
copySummaryButton.classList.remove("hidden");
tagContainer.innerHTML = "";
const tags = result[0].result["tags"] || [];
if (tags.length == 0) {
tagContainer.innerHTML = "Tags not available";
}
tags.forEach(tag => {
const chip = document.createElement("span");
chip.className = "chip";
chip.innerText = tag;
tagContainer.appendChild(chip);
});
const sentiment =result[0].result["sentiment"];
sentimentContainer.className = "";
switch (sentiment) {
case 1:
sentimentContainer.innerText = "\u263A";
sentimentContainer.style.color = "green";
break;
case 0:
sentimentContainer.innerText = "\uD83D\uDE10";;
break;
case -1:
sentimentContainer.innerText = "\u2639";
sentimentContainer.style.color = "Red";
break;
default:
sentimentContainer.innerText = "Sentiment not available";
sentimentContainer.style.color = "";
break;
}
copyCitationButton.addEventListener("click", () => {
const citation = `Citation: ${activeTab.title}. Retrieved from ${activeTab.url}`;
navigator.clipboard.writeText(citation).then(() => {
const originalButton = copyCitationButton.innerText;
copyCitationButton.innerText = "Copied!";
setTimeout(() => {
copyCitationButton.innerText = originalButton;
}, 2000);
}, () => {
alert("Failed to copy citation!");
});
});
}
else {
summaryContainer.innerText = "Summary not available";
tagContainer.innerText = "Tags not available";
sentimentContainer.innerText = "Sentiment not available";
copyCitationButton.classList.add("hidden");
copySummaryButton.classList.add("hidden");
}
});
copySummaryButton.addEventListener("click", () => {
if (isSummary) {
navigator.clipboard.writeText(summaryText).then(() => {
const originalButton = copySummaryButton.innerText;
copySummaryButton.innerText = "Copied!";
setTimeout(() => {
copySummaryButton.innerText = originalButton;
}, 2000);
}, () => {
alert("Failed to copy summary.");
});
} else {
alert("No summary available to copy.");
}
});
});
async function getActiveTab() {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
return tab;
}
async function summarize(numSentences, targetLang) {
const message = window.location.href;
const data = { message: message, numSentences: numSentences, targetLang: targetLang };
try {
const response = await fetch("http://localhost:8000/summarize", {
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8",
"Accept-Charset": "utf-8"
},
body: JSON.stringify(data),
});
const result = await response.json();
return result;
} catch (error) {
console.error(error);
}
}