-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbutton_popup.js
More file actions
57 lines (53 loc) · 1.86 KB
/
button_popup.js
File metadata and controls
57 lines (53 loc) · 1.86 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
document.addEventListener('DOMContentLoaded', () => {
$("submitButton").addEventListener("click", (event) => {
sendMessage();
});
document.addEventListener("keypress", (event) => {
if (event.key === 'Enter') {
event.preventDefault();
sendMessage();
}
});
chrome.storage.local.get(null).then((/** @param {Options} result */result) => {
const promptsDropdown = $("availablePrompts");
for (let i = 0; i < result.promptData.length; i++){
const prompt = result.promptData[i];
if (!prompt.enabled) {
continue;
}
const option = document.createElement("option");
option.text = prompt.title;
option.value = i.toString();
if (option.value === result.buttonPopupSelectedPrompt) {
option.selected = true;
}
promptsDropdown.appendChild(option);
}
});
$("availablePrompts").addEventListener("change", (event) => {
chrome.storage.local.set({"buttonPopupSelectedPrompt": event.target.value});
})
});
function getSelectedText() {
return window.getSelection().toString();
}
function closeThisPopup() {
window.close();
}
function sendMessage() {
const userText = $("myInput").value;
const promptId = document.getElementById("availablePrompts").value;
chrome.tabs.query({active: true, lastFocusedWindow: true}, function (tabs) {
chrome.scripting.executeScript({
target: {tabId: tabs[0].id}, function: getSelectedText,
}, (results) => {
let selectedText = "";
if (results.length > 0) selectedText = results[0].result;
// Now that we have the selected text, we can send it to background.js
chrome.runtime.sendMessage({
action: 'ext_button_message', userText: userText, tab: tabs[0], selectedText: selectedText, promptId: promptId,
});
closeThisPopup();
});
});
}