-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcontent.js
173 lines (151 loc) · 4.4 KB
/
content.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// content.js
let waiMenu = null;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "openWaiMenu") {
toggleWaiMenu();
}
});
function toggleWaiMenu() {
if (waiMenu) {
document.body.removeChild(waiMenu);
waiMenu = null;
} else {
createWaiMenu();
}
}
function createWaiMenu() {
waiMenu = document.createElement("div");
waiMenu.id = "wai-menu";
waiMenu.innerHTML = `
<div class="wai-menu-container">
<input type="text" id="wai-menu-input" placeholder="Type a command or search...">
<div id="wai-menu-options"></div>
</div>
`;
document.body.appendChild(waiMenu);
const input = waiMenu.querySelector("#wai-menu-input");
input.focus();
input.addEventListener("input", handleInput);
input.addEventListener("keydown", handleKeydown);
document.addEventListener("click", handleOutsideClick);
populateOptions(defaultOptions);
}
function handleInput(event) {
const query = event.target.value.toLowerCase();
const filteredOptions = defaultOptions.filter(
(option) =>
option.name.toLowerCase().includes(query) || option.description.toLowerCase().includes(query)
);
populateOptions(filteredOptions);
}
function handleKeydown(event) {
if (event.key === "Enter") {
const selectedOption = waiMenu.querySelector(".wai-menu-option.selected");
if (selectedOption) {
executeOption(selectedOption.dataset.action);
}
} else if (event.key === "ArrowDown" || event.key === "ArrowUp") {
event.preventDefault();
navigateOptions(event.key === "ArrowDown" ? 1 : -1);
} else if (event.key === "Escape") {
toggleWaiMenu();
}
}
function handleOutsideClick(event) {
if (waiMenu && !waiMenu.contains(event.target)) {
toggleWaiMenu();
}
}
function populateOptions(options) {
const optionsContainer = waiMenu.querySelector("#wai-menu-options");
optionsContainer.innerHTML = options
.map(
(option, index) => `
<div class="wai-menu-option ${index === 0 ? "selected" : ""}" data-action="${option.action}">
<div class="wai-menu-option-name">${option.name}</div>
<div class="wai-menu-option-description">${option.description}</div>
</div>
`
)
.join("");
}
function navigateOptions(direction) {
const options = waiMenu.querySelectorAll(".wai-menu-option");
const currentIndex = Array.from(options).findIndex((option) =>
option.classList.contains("selected")
);
options[currentIndex].classList.remove("selected");
const newIndex = (currentIndex + direction + options.length) % options.length;
options[newIndex].classList.add("selected");
options[newIndex].scrollIntoView({ block: "nearest" });
}
function executeOption(action) {
const selectedText = window.getSelection().toString();
chrome.runtime.sendMessage({ action, selectedText });
toggleWaiMenu();
}
const defaultOptions = [
{ name: "Summarize", action: "summarize", description: "Summarize the selected text" },
{ name: "Translate", action: "translate", description: "Translate the selected text" },
{ name: "Explain", action: "explain", description: "Explain the selected text in simple terms" },
{
name: "Find Keywords",
action: "keywords",
description: "Extract key words from the selected text",
},
{
name: "Sentiment Analysis",
action: "sentiment",
description: "Analyze the sentiment of the selected text",
},
];
// Inject CSS
const style = document.createElement("style");
style.textContent = `
#wai-menu {
position: fixed;
top: 20%;
left: 50%;
transform: translateX(-50%);
width: 600px;
max-width: 90%;
background-color: #1a1a1a;
border: 1px solid #333;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
z-index: 10000;
font-family: 'Roboto Mono', monospace;
}
.wai-menu-container {
padding: 10px;
}
#wai-menu-input {
width: 100%;
padding: 10px;
border: none;
background-color: #2a2a2a;
color: #fff;
font-size: 16px;
font-family: 'Roboto Mono', monospace;
}
#wai-menu-options {
max-height: 300px;
overflow-y: auto;
}
.wai-menu-option {
padding: 10px;
cursor: pointer;
border-top: 1px solid #333;
}
.wai-menu-option:hover, .wai-menu-option.selected {
background-color: #2a2a2a;
}
.wai-menu-option-name {
font-weight: bold;
color: #fff;
}
.wai-menu-option-description {
font-size: 14px;
color: #aaa;
}
`;
document.head.appendChild(style);