forked from lalit10/SimplyClip
-
Notifications
You must be signed in to change notification settings - Fork 2
/
popup.js
446 lines (417 loc) · 16.3 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
MIT License
Copyright (c) 2021 lalit10
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
let _clipboardList = document.querySelector("#clipboard_list");
let addButton = document.getElementById('add-btn');
addButton.addEventListener('click', (event) => {
let textitem = ''
let emptyDiv = document.getElementById('empty-div');
let downloadDiv1 = document.getElementById('download-btn1');
let downloadDiv2 = document.getElementById('download-btn2');
let searchInput = document.getElementById('searchText');
emptyDiv.classList.add('hide-div');
downloadDiv1.style.display = 'block';
downloadDiv2.style.display = 'block';
document.getElementsByClassName('doc')[0].addEventListener('click', (event) => {
downloadClipboardTextAsDoc()
})
document.getElementsByClassName('csv')[0].addEventListener('click', (event) => {
downloadClipboardTextAsCsv()
})
searchInput.style.display = 'block';
searchInput.addEventListener('keyup', () => {
searchClipboardText();
})
chrome.storage.sync.get(['list'], text => {
let list = text.list;
list == undefined && (list = []);
list.unshift("");
chrome.storage.sync.set({ 'list': list })
})
chrome.storage.sync.get(['listURL'], url => {
let urlList = url.listURL;
urlList == undefined && (urlList = []);
urlList.unshift("");
chrome.storage.sync.set({ 'listURL': urlList })
})
chrome.storage.sync.get(['originalList'], original => {
let originalList = original.originalList;
originalList == undefined && (originalList = []);
originalList.unshift("");
chrome.storage.sync.set({ 'originalList': originalList })
})
addClipboardListItem(textitem)
}
)
/**
* Gets the copied text from storage and calls addClipboardListItem function to populate list in the pop-up
* @example
* getClipboardText()
*/
function getClipboardText() {
chrome.storage.sync.get(['list'], clipboard => {
let list = clipboard.list;
let emptyDiv = document.getElementById('empty-div');
let downloadDiv1 = document.getElementById('download-btn1');
let downloadDiv2 = document.getElementById('download-btn2');
let searchInput = document.getElementById('searchText');
let deleteAll = document.getElementById('delete-btn');
if (list === undefined || list.length === 0) {
emptyDiv.classList.remove('hide-div');
downloadDiv1.style.display = 'none';
downloadDiv2.style.display = 'none';
searchInput.style.display = 'none';
deleteAll.style.display = 'none';
}
else {
emptyDiv.classList.add('hide-div');
downloadDiv1.style.display = 'block';
downloadDiv2.style.display = 'block';
deleteAll.style.display = 'block';
document.getElementsByClassName('doc')[0].addEventListener('click', (event) => {
downloadClipboardTextAsDoc()
})
document.getElementsByClassName('csv')[0].addEventListener('click', (event) => {
downloadClipboardTextAsCsv()
})
searchInput.style.display = 'block';
searchInput.addEventListener('keyup', () => {
searchClipboardText();
})
deleteAll.addEventListener('click',() => {
deleteAllText();
})
if (typeof list !== undefined)
list.forEach(item => {
addClipboardListItem(item)
});
}
});
}
/**
* Gets the source URL and the image url for the copied image/text
* @param {*} textContent
* @returns
*/
function getThumbnail(textContent) {
let ind = textContent.indexOf('https://www.youtube.com/');
if (ind === 0) {
let videoId = "";
let idIndex = textContent.indexOf('watch?v=');
let endIndex = textContent.indexOf('&');
if (endIndex !== -1)
videoId = textContent.substring(idIndex + 8, endIndex);
else
videoId = textContent.substring(idIndex + 8, textContent.length);
let url = `https://img.youtube.com/vi/${videoId}/1.jpg`;
return {
sourceUrl: textContent,
imageUrl: url,
isVideo: true,
};
}
else {
let ind = textContent.indexOf('http');
if (ind === 0) {
let url = new URL(textContent);
let ans = "https://favicons.githubusercontent.com/" + url.hostname;
return {
sourceUrl: textContent,
imageUrl: ans,
isVideo: false
}
}
}
return {
sourceUrl: "",
imageUrl: ""
}
;
}
/**
* Creates an HTML li element and adds the input text, icon to edit, icon to delete
* Contains click event listeners for edit and delete icon
* @param {*} text
* @example
* addClipboardListItem("123")
*/
function addClipboardListItem(text) {
let { sourceUrl, imageUrl, isVideo } = getThumbnail(text);
let listItem = document.createElement("li"),
listDiv = document.createElement("div"),
deleteDiv = document.createElement("div"),
editDiv = document.createElement("div"),
contentDiv = document.createElement("div"),
editImage = document.createElement("img");
editImage.setAttribute("data-toggle", "tooltip");
editImage.setAttribute("data-placement", "bottom");
editImage.setAttribute("title", "Click to edit the text entry!");
let deleteImage = document.createElement("img");
deleteImage.setAttribute("data-toggle", "tooltip");
deleteImage.setAttribute("data-placement", "bottom");
deleteImage.setAttribute("title", "Click to delete the text entry!");
let listPara = document.createElement("p");
let listText = document.createTextNode(text);
listPara.setAttribute("data-toggle", "tooltip");
listPara.setAttribute("data-placement", "bottom");
listPara.setAttribute("title", "Click to copy the below text:\n" + text);
let popupLink = document.createElement('a');
let imagePopup = document.createElement('img');
prevText = text;
if (imageUrl.length > 0) {
console.log("IMage Url found")
imagePopup.src = imageUrl;
if (!isVideo) {
imagePopup.style.width = '32px'
imagePopup.style.height = '32px';
}
else {
imagePopup.style['margin-left'] = '0px';
imagePopup.style['margin-top'] = '0px';
listPara.style['max-width'] = '12rem'
}
popupLink.href = sourceUrl;
popupLink.target = '_blank';
popupLink.appendChild(imagePopup);
listDiv.appendChild(popupLink);
}
listPara.appendChild(listText)
listDiv.appendChild(listPara);
listPara.addEventListener('focusout', (event) => {
event.target.setAttribute("contenteditable", "false");
listPara.style.height = '4em';
listPara.style.whiteSpace = 'inherit'
newText = event.target.textContent;
chrome.storage.sync.get(['list'], clipboard => {
let list = clipboard.list;
let index = list.indexOf(prevText);
list[index] = newText;
chrome.storage.sync.set({ 'list': list }, () => { console.log("Text updated"); });
})
})
listDiv.classList.add("list-div");
contentDiv.appendChild(listDiv);
editImage.src = './images/pencil.png';
editImage.classList.add("edit");
deleteImage.src = './images/delete-note.png';
// deleteImage.src = 'https://cdn.iconscout.com/icon/premium/png-256-thumb/delete-1432400-1211078.png'
deleteImage.classList.add("delete")
editDiv.appendChild(editImage);
contentDiv.appendChild(editDiv);
deleteDiv.appendChild(deleteImage);
contentDiv.appendChild(deleteDiv);
contentDiv.classList.add("content");
listItem.appendChild(contentDiv);
_clipboardList.appendChild(listItem);
editImage.addEventListener('click', (event) => {
console.log("Edit button clicked");
prevText = listPara.textContent;
console.log(prevText);
listPara.setAttribute("contenteditable", "true");
listPara.style.height = 'auto';
listPara.style.whiteSpace = 'break-spaces';
listPara.focus();
// listDiv.style.borderColor = "red";
// listPara.style.backgroundColor = "grey"
// listPara.style.height = "100px"
//listPara.focus();
})
deleteImage.addEventListener('click', (event) => {
console.log("Delete clicked");
chrome.storage.sync.get(['list'], clipboard => {
let list = clipboard.list;
let index = list.indexOf(text);
list.splice(index, 1);
_clipboardList.innerHTML = "";
chrome.storage.sync.get(['listURL'], url => {
let urlList = url.listURL;
urlList.splice(index, 1);
chrome.storage.sync.set({ 'listURL': urlList })
})
chrome.storage.sync.get(['originalList'], original => {
let originalList = original.originalList;
originalList.splice(index, 1);
chrome.storage.sync.set({ 'originalList': originalList })
})
chrome.storage.sync.set({ 'list': list }, () => getClipboardText());
})
})
listDiv.addEventListener('click', (event) => {
let { textContent } = event.target;
navigator.clipboard.writeText(textContent)
.then(() => {
console.log(`Text saved to clipboard`);
chrome.storage.sync.get(['list'], clipboard => {
let list = clipboard.list;
let index = list.indexOf(textContent);
if (index !== -1)
list.splice(index, 1);
list.unshift(textContent);
_clipboardList.innerHTML = "";
chrome.storage.sync.set({ 'list': list }, () => getClipboardText());
});
});
let x = document.getElementById("snackbar");
x.className = "show";
setTimeout(function () { x.className = x.className.replace("show", ""); }, 3000);
});
}
/**
* Retrives the copied text from the storage ,
* generates a doc file and
* downloads the file
* @example
* downloadClipboardTextAsDoc()
*/
function downloadClipboardTextAsDoc(){
chrome.storage.sync.get(['list'], clipboard => {
let list = clipboard.list;
let emptyDiv = document.getElementById('empty-div');
if (list === undefined || list.length === 0) {
emptyDiv.classList.remove('hide-div');
console.log("Nothing to download")
}
else {
var list_of_items = []
emptyDiv.classList.add('hide-div');
if (typeof list !== undefined){
list.forEach(item => {
list_of_items = list_of_items + item + "\n\n"
});
var link, blob, url;
blob = new Blob(['\ufeff', list_of_items], {
type: 'application/msword'
});
url = URL.createObjectURL(blob);
link = document.createElement('A');
link.href = url;
link.download = 'SimplyClip';
document.body.appendChild(link);
if (navigator.msSaveOrOpenBlob )
navigator.msSaveOrOpenBlob( blob, 'SimplyClip.doc');
else link.click(); // other browsers
document.body.removeChild(link);
}
}
});
}
/**
* Filters the
* displayed copied text list that matches search text in the search box
* @example
* searchClipboardText()
*/
function searchClipboardText() {
var input, filter, ul, li, a, i, txtValue;
input = document.getElementById("searchText");
filter = input.value.toUpperCase();
ul = document.getElementById("clipboard_list");
li = ul.getElementsByTagName("li");
for (i = 0; i < li.length; i++) {
divElement = li[i].getElementsByClassName("list-div")[0];
let elementText = divElement.getElementsByTagName('p')[0]
txtValue = elementText.textContent || elementText.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
var enabled = false;
var myButton = document.getElementById('toggle-button');
chrome.storage.local.get('enabled', data => {
var myButton = document.getElementById('toggle-button');
enabled = !!data.enabled;
switchButton = document.getElementsByClassName('switch')[0]
if(enabled==true){
myButton.checked = enabled
switchButton.title="Click here to disable saving your copied text!!"
}
else{
myButton.checked = enabled
switchButton.title="Click here to save your copied text!!"
}
});
myButton.onchange = () => {
enabled = !enabled;
switchButton = document.getElementsByClassName('switch')[0]
if(enabled==true){
myButton.checked = enabled
switchButton.title="Click here to disable saving your copied text!!"
}
else{
myButton.checked = enabled
switchButton.title="Click here to save your copied text!!"
}
chrome.storage.local.set({enabled:enabled});
};
getClipboardText();
/**
* Retrives the copied text, original copied text and URL of copied text from the storage ,
* generates a CSV file and
* downloads the file
* @example
* downloadClipboardTextAsCsv()
*/
function downloadClipboardTextAsCsv() {
let data = [];
chrome.storage.sync.get(['list'], clipboard => {
clipboardData = clipboard.list
chrome.storage.sync.get(['listURL'], url => {
urlData = url.listURL
chrome.storage.sync.get(['originalList'], original => {
originalData = original.originalList
clipboardData.forEach((d, index) => {
let rowData = [];
rowData.push(d)
rowData.push(originalData[index])
rowData.push(urlData[index])
data.push(rowData)
})
var csv = 'Edited Text,OriginalText,URL\n';
data.forEach(function (row) {
for (let i in row) {
row[i] = row[i].replace(/"/g, '""');
}
csv += '"' + row.join('","') + '"';
csv += "\n";
});
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
hiddenElement.target = '_blank';
hiddenElement.download = 'simplyClip.csv';
hiddenElement.click();
})
})
})
}
/**
* Deletes all the text copied in the simplyclip clipboard
* @example
* deleteAllText()
*/
function deleteAllText() {
chrome.storage.sync.set({ 'list': [] }, () => {});
chrome.storage.sync.set({ 'originalList': [] }, () => {});
chrome.storage.sync.set({ 'listURL': [] }, () => {});
getClipboardText();
var ul = document.getElementById("clipboard_list");
ul.innerHTML = "";
}