-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
155 lines (136 loc) · 5 KB
/
popup.js
File metadata and controls
155 lines (136 loc) · 5 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* @file popup.js
* @description Logic for the extension's popup UI.
*/
class PopupManager {
constructor() {
this.initialize();
}
async initialize() {
this.setupEventListeners();
this.showLoading('Initializing...');
try {
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true
});
if (tab && tab.url && tab.url.startsWith('http')) {
const response = await chrome.tabs.sendMessage(tab.id, {
action: 'extractContent'
});
if (response && response.success) {
this.pageContent = response.data;
}
} else {
this.showError("Cannot run on this page.");
}
} catch (error) {
console.warn('Could not extract page content:', error);
this.showError("Failed to access page content.");
} finally {
this.hideLoading();
}
}
setupEventListeners() {
document.getElementById('summarize-page').addEventListener('click', () => this.summarizePage());
document.getElementById('ask-button').addEventListener('click', () => this.askQuestion());
document.querySelectorAll('.tab-button').forEach(button => {
button.addEventListener('click', e => this.switchTab(e.currentTarget.dataset.tab));
});
}
switchTab(tabName) {
document.querySelectorAll('.tab-button.active, .tab-pane.active').forEach(el => el.classList.remove('active'));
document.querySelector(`.tab-button[data-tab="${tabName}"]`).classList.add('active');
document.getElementById(`${tabName}-tab`).classList.add('active');
}
async summarizePage() {
if (!this.pageContent) {
this.showError("No content to summarize.");
return;
}
this.showLoading("Generating summary...");
try {
const response = await chrome.runtime.sendMessage({
action: 'summarize',
data: {
text: this.pageContent.mainText,
type: document.getElementById('summary-type').value,
length: document.querySelector('input[name="summary-length"]:checked').value,
}
});
if (response.success) {
this.displayResult('summary-text', response.data);
} else {
throw new Error(response.error);
}
} catch (error) {
this.showError(`Summarization failed: ${error.message}`);
} finally {
this.hideLoading();
}
}
async askQuestion() {
const questionInput = document.getElementById('question-input');
const question = questionInput.value.trim();
if (!question) return;
if (!this.pageContent) {
this.showError("No page content for context.");
return;
}
this.showLoading("Thinking...");
this.addChatMessage('user', question);
questionInput.value = '';
try {
const response = await chrome.runtime.sendMessage({
action: 'ask-question',
data: {
question,
context: this.pageContent.mainText,
}
});
if (response.success) {
this.addChatMessage('assistant', response.data);
} else {
throw new Error(response.error);
}
} catch (error) {
this.addChatMessage('assistant', `Error: ${error.message}`);
} finally {
this.hideLoading();
}
}
displayResult(elementId, text) {
document.getElementById(elementId).innerHTML = this.formatText(text);
}
addChatMessage(role, text) {
const chatHistory = document.getElementById('chat-history');
const messageEl = document.createElement('div');
messageEl.classList.add('chat-message', role);
messageEl.innerHTML = this.formatText(text);
chatHistory.appendChild(messageEl);
chatHistory.scrollTop = chatHistory.scrollHeight; // Auto-scroll
}
formatText(text) {
return text
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/\n/g, '<br>');
}
showLoading(message) {
const overlay = document.getElementById('loading-overlay');
document.getElementById('loading-text').textContent = message;
overlay.style.display = 'flex';
}
hideLoading() {
document.getElementById('loading-overlay').style.display = 'none';
}
showError(message) {
this.updateStatus('error', message);
}
updateStatus(type, text) {
const statusText = document.getElementById('status-text');
statusText.textContent = text;
statusText.className = `status-text ${type}`;
}
}
document.addEventListener('DOMContentLoaded', () => new PopupManager());