-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
135 lines (119 loc) · 5.08 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
document.addEventListener('DOMContentLoaded', () => {
const newText = document.getElementById('newText');
const addText = document.getElementById('addText');
const textList = document.getElementById('textList');
const exportTexts = document.getElementById('exportTexts');
const importTexts = document.getElementById('importTexts');
const importFile = document.getElementById('importFile');
const searchInput = document.getElementById('searchInput');
searchInput.focus();
function updateUI() {
chrome.storage.local.get('texts', ({ texts }) => {
if (!Array.isArray(texts) || !texts.length) {
chrome.storage.local.set({ 'texts': [] });
}
});
chrome.storage.local.get('texts', ({ texts }) => {
textList.innerHTML = '';
(texts || []).forEach((text, index) => {
const listItem = document.createElement('li');
listItem.classList.add('list-group-item', 'justify-content-between', 'align-items-center');
listItem.textContent = text;
listItem.style.cursor = 'pointer';
listItem.style.display = 'flex';
listItem.addEventListener('click', () => {
navigator.clipboard.writeText(text);
try {
chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
chrome.scripting.executeScript({
target: {
tabId: tabs[0].id
},
args: [text],
function: function (text) {
let textarea = document.querySelector("textarea.m-0.w-full.resize-none.border-0.bg-transparent");
if (textarea) {
textarea.value = text;
textarea.dispatchEvent(new Event('input', { bubbles: true }));
}
},
});
});
window.close();
} catch (error) {
console.log(error);
}
});
const deleteButton = document.createElement('button');
deleteButton.classList.add('btn', 'btn-danger', 'btn-sm', 'float-right');
deleteButton.textContent = 'X';
deleteButton.addEventListener('click', (e) => {
e.stopPropagation();
e.preventDefault();
texts.splice(index, 1);
chrome.storage.local.set({ texts }, updateUI);
});
listItem.appendChild(deleteButton);
textList.appendChild(listItem);
});
});
}
addText.addEventListener('click', () => {
const text = newText.value;
if (text) {
chrome.storage.local.get('texts', ({ texts }) => {
texts.push(text);
chrome.storage.local.set({ texts }, updateUI);
newText.value = '';
});
}
});
exportTexts.addEventListener('click', () => {
chrome.storage.local.get('texts', ({ texts }) => {
const fileContent = JSON.stringify({ texts }, null, 2);
const file = new Blob([fileContent], { type: 'application/json' });
const url = URL.createObjectURL(file);
const link = document.createElement('a');
link.href = url;
link.download = 'texts_export.json';
link.click();
URL.revokeObjectURL(url);
});
});
importTexts.addEventListener('click', () => {
importFile.click();
});
importFile.addEventListener('change', () => {
const file = importFile.files[0];
if (file) {
const reader = new FileReader();
reader.readAsText(file);
reader.onload = () => {
try {
const { texts } = JSON.parse(reader.result);
if (Array.isArray(texts)) {
chrome.storage.local.set({ texts }, updateUI);
}
} catch (error) {
console.error('Invalid JSON file:', error.message);
}
};
}
});
searchInput.addEventListener('input', () => {
let list = textList.querySelectorAll('li');
list.forEach((item) => {
let itemTextLower = item.textContent.toLowerCase();
let searchWords = searchInput.value.toLowerCase().split(/\s+/g).filter(word => word !== '');
if (searchWords.every(word => itemTextLower.includes(word))) {
item.style.display = 'flex';
} else {
item.style.display = 'none';
}
});
});
updateUI();
});