-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
83 lines (71 loc) · 2.88 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
const spinner = document.getElementById('spinner');
const checkmark = document.getElementById('checkmark');
const copyButton = document.getElementById('copyButton');
const pasteButton = document.getElementById('pasteButton');
// Copy button event listener
copyButton.addEventListener('click', () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { action: 'writeToClipboard', responseText: instructions.value });
});
});
// Paste button event listener
pasteButton.addEventListener('click', () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { action: 'readClipboard' });
});
});
document.addEventListener('DOMContentLoaded', () => {
const popupPort = chrome.runtime.connect({ name: "popup" });
const instructions = document.getElementById('instructions');
const processTextButton = document.getElementById('processText');
const enableDisableButton = document.getElementById('enableDisable');
const openGHButton = document.getElementById('open-gh-button');
// Load saved instructions from storage
chrome.storage.sync.get('instructions', (data) => {
if (data.instructions) {
instructions.value = data.instructions;
}
});
// Save instructions to storage when modified
instructions.addEventListener('input', () => {
chrome.storage.sync.set({ 'instructions': instructions.value });
});
// processTextButton click event listener in popup.js
processTextButton.addEventListener("click", () => {
// Show spinner and hide checkmark
spinner.classList.remove("hidden");
checkmark.classList.add("hidden");
// Send a message to the content script to read the clipboard
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { action: "readClipboard" });
});
});
// Listen for messages from the background page
chrome.runtime.onMessage.addListener((message) => {
if (message.action === "processingComplete") {
console.log("Received processingComplete message");
spinner.classList.add("hidden");
checkmark.classList.remove("hidden");
// Hide checkmark after 3 seconds
setTimeout(() => {
checkmark.classList.add("hidden");
}, 3000);
}
});
// Enable or disable the extension
let isEnabled = true;
enableDisableButton.addEventListener('click', () => {
isEnabled = !isEnabled;
enableDisableButton.textContent = isEnabled ? 'Disable' : 'Enable';
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, {
action: 'toggleExtension',
enabled: isEnabled
});
});
});
// open github page button
openGHButton.addEventListener('click', () => {
chrome.tabs.create({ url: 'https://github.com/aristotle-tek/AI-Clip-Response' });
});
});