-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.js
509 lines (451 loc) · 20.4 KB
/
init.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
Initializes global variables and functions.
*/
// Detect and override input elements that uses .click()
var oriClick = HTMLElement.prototype.click;
HTMLElement.prototype.click = function(...args) {
if (this.matches("input[type='file']") && !this.id.toLowerCase().startsWith('cnp')) {
setupcreateOverlay(this);
oriClick.call(this, ...args);
} else
return oriClick.apply(this, arguments);
};
// Detect and override input elements that uses .showPicker()
var oriShowPicker = HTMLInputElement.prototype.showPicker;
HTMLInputElement.prototype.showPicker = function() {
if (this.matches("input[type='file']"))
this.click();
else
return oriShowPicker.apply(this, arguments);
};
// Global variables
var clientX = 0;
var clientY = 0;
var overlayID = null;
var ctrlVdata = null;
var currentObjectURL = null;
var reader = null; //Paste event listener's
var isFirefox = typeof InstallTrigger !== 'undefined';
var isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
// Capture cursor coords for overlay position
if (!document.cnpCoordListener)
document.addEventListener('mousemove', event => {
document.cnpCoordListener = true;
clientX = event.clientX;
clientY = event.clientY;
});
// Sets a node up for CnP Overlay
function setupcreateOverlay(node) {
if (node.id != "cnp-overlay-file-input" && !node.dataset.cnpCreateListener) {
node.addEventListener("click", createOverlay);
node.dataset.cnpCreateListener = "true";
}
}
// Ctrl V listener
async function ctrlV(event) {
const overlayContent = document.querySelector('.cnp-overlay-content');
if (overlayContent && event.ctrlKey && (event.key === 'v' || event.key === 'V')) {
event.preventDefault();
if (ctrlVdata && ctrlVdata.files) {
const fileList = new DataTransfer();
// Include previously selected files for multi-file
[...originalInput.files].forEach(file => fileList.items.add(file));
// Append pasted files
const readPromise = [...ctrlVdata.files]
.filter(blob => !(blob.size === 0 && blob.type === ''))
.map(blob => {
return new Promise(resolve => {
const reader = new FileReader();
let fileName = blob.name;
if (blob.name == 'image.png' || !blob.name)
fileName = 'CnP_'+new Date().toLocaleString('en-GB', {hour12: false}).replace(/, /g, '_').replace(/[\/: ]/g, '')+'.'+blob.type.split('/').pop();
reader.onload = () => {
const file = new File([blob], fileName, {type: blob.type, lastModified: blob.lastModified});
fileList.items.add(file);
resolve();
};
reader.readAsArrayBuffer(blob);
});
});
await Promise.all(readPromise);
originalInput.files = fileList.files;
triggerChangeEvent(originalInput);
closeOverlay();
}
}
}
// Preview copied image in overlay
function previewImage(webCopiedImgSrc, readerEvent, blob, requestedOverlayID) {
if (overlayID !== requestedOverlayID)
return;
let imagePreview = document.querySelector('#cnp-image-preview');
const imagePreviewContainer = document.querySelector('#cnp-preview-container');
const spinner = document.querySelector('.cnp-spinner');
if (!imagePreview) {
// Preview image types
if (blob.type.split('/')[0] == 'image') {
imagePreview = document.createElement('img');
imagePreview.id = 'cnp-image-preview';
currentObjectURL = window.URL.createObjectURL(new Blob([readerEvent.target.result], {type: blob.type}));
imagePreview.src = currentObjectURL;
try {imagePreviewContainer.appendChild(imagePreview)} catch (error) {logging(error)}
imagePreview.onload = () => {
// Enlarge preview of smaller images
if ((imagePreview.naturalWidth < 272 && imagePreview.naturalHeight < 153) || imagePreview.naturalHeight < 153 && imagePreview.naturalWidth <= imagePreview.naturalHeight) {
imagePreview.style.width = "auto";
imagePreview.style.height = "100%";
} else if (imagePreview.naturalWidth < 272 && imagePreview.naturalWidth > imagePreview.naturalHeight) {
imagePreview.style.width = "100%";
imagePreview.style.height = "auto";
}
spinner.style.display = 'none';
}
}
// Preview PDF type
else if (blob.type.split('/').pop() == 'pdf') {
spinner.style.display = 'none';
imagePreview = document.createElement('iframe');
imagePreview.id = 'cnp-image-preview';
imagePreview.type = blob.type;
currentObjectURL = window.URL.createObjectURL(new Blob([readerEvent.target.result], {type: blob.type})) + '#scrollbar=0&view=FitH,top&page=1&toolbar=0&statusbar=0&navpanes=0';
imagePreview.src = currentObjectURL;
try {imagePreviewContainer.appendChild(imagePreview)} catch (error) {logging(error)}
spinner.style.display = 'none';
}
// Preview video types
else if (blob.type.split('/')[0] == 'video') {
spinner.style.display = 'none';
imagePreview = document.createElement('video');
imagePreview.id = 'cnp-image-preview';
imagePreview.preload = "metadata";
imagePreview.type = blob.type;
currentObjectURL = window.URL.createObjectURL(new Blob([readerEvent.target.result], {type: blob.type}));
imagePreview.src = currentObjectURL;
imagePreview.onloadedmetadata = () => {
if (imagePreview.videoWidth == 0 || imagePreview.videoHeight == 0)
previewGenericFile('audio_file');
else
try {imagePreviewContainer.appendChild(imagePreview)} catch (error) {logging(error)}
}
spinner.style.display = 'none';
}
// Preview audio type
else if (blob.type.split('/')[0] == 'audio')
previewGenericFile('audio_file');
else if (blob.type.split('/')[0] == 'text')
previewGenericFile('text_file');
// Preview other file types
else
previewGenericFile('blank_file');
}
function previewGenericFile(fileTypeIcon) {
imagePreview = document.createElement('img');
imagePreview.id = 'cnp-image-preview';
imagePreview.style.height = '50%';
try {
imagePreview.src = chrome.runtime.getURL(`media/${fileTypeIcon}.webp`);
} catch {
try {
if (document.head.querySelector('script:is([id*="CnP-mutatedIframe"], [id*="CnP-iframe"])'))
window.top.postMessage({'Type': 'getURL', 'iframe': document.head.querySelector('script:is([id*="CnP-mutatedIframe"], [id*="CnP-iframe"])').getAttribute('id'), 'Path': `media/${fileTypeIcon}.webp`}, '*');
else
window.top.postMessage({'Type': 'getURL', 'Path': `media/${fileTypeIcon}.webp`}, '*');
window.onmessage = event => {
if (event.data.Type == 'getURL-response')
imagePreview.src = event.data.URL;
}
} catch (error) {logging(error)}
}
try {imagePreviewContainer.appendChild(imagePreview)} catch (error) {logging(error)}
let title = document.createElement('span');
const extension = blob.name.split('.').pop();
let baseName = blob.name.slice(0, -extension.length - 1);
if (blob.name.length > 25) {
baseName = baseName.slice(0, 25);
// Remove last character if it's a whitespace
while (baseName[baseName.length - 1] === ' ')
baseName = baseName.slice(0, -1);
baseName += '..';
}
title.textContent = `${baseName}.${extension}`;
title.id = 'cnp-image-title';
imagePreviewContainer.appendChild(title);
spinner.style.display = 'none';
}
// Preview GIF images (copied from web)
// if (webCopiedImgSrc.endsWith('.gif')) {
// img.src = webCopiedImgSrc;
// }
// if (webCopiedImgSrc.endsWith('.gif'))
// fileName = fileName.replace('.png', '.gif');
}
// When prepped input elements are clicked
function createOverlay(event) {
event.preventDefault();
originalInput = event.target;
// Create overlay
const overlay = document.createElement('div');
overlay.classList.add('cnp-overlay');
// Unique ID for each created overlay
const hexArray = Array.from(crypto.getRandomValues(new Uint8Array(16))).map(byte => byte.toString(16).padStart(2, '0'));
const uuid = [hexArray.slice(0, 4).join(''), hexArray.slice(4, 6).join(''), '4' + hexArray.slice(6, 7).join(''), (parseInt(hexArray[8], 16) & 0x3 | 0x8).toString(16) + hexArray.slice(9, 11).join(''), hexArray.slice(11, 16).join('')].join('-');
overlay.id = overlayID = uuid;
// Fetch overlay.html
function fetchURL() {
return new Promise(resolve => {
var urlToFetch = null;
if (document.head.querySelector('script[overlayhtml]')) {
urlToFetch = document.head.querySelector('script[overlayhtml]').getAttribute('overlayhtml');
resolve(urlToFetch);
}
else if (!urlToFetch && typeof chrome.runtime !== 'undefined')
try {
urlToFetch = chrome.runtime.getURL('overlay.html');
resolve(urlToFetch);
}
catch {
if (document.head.querySelector('script:is([id*="CnP-mutatedIframe"], [id*="CnP-iframe"])'))
window.top.postMessage({'Type': 'getURL', 'iframe': document.head.querySelector('script:is([id*="CnP-mutatedIframe"], [id*="CnP-iframe"])').getAttribute('id'), 'Path': 'overlay.html'}, '*');
else
window.top.postMessage({'Type': 'getURL', 'Path': 'overlay.html'}, '*');
window.onmessage = event => {
if (event.data.Type == 'getURL-response') {
urlToFetch = event.data.URL;
resolve(urlToFetch);
}
}
}
else if (document.head.querySelector('copy-n-paste'))
try {
urlToFetch = document.head.querySelector('copy-n-paste').getAttribute('overlay-html');
resolve(urlToFetch);
} catch (error) {logging(error)}
else
resolve(urlToFetch);
});
}
try {
fetchURL().then(urlToFetch => {
if (urlToFetch) {
fetch(urlToFetch)
.then(response => response.text())
.then(html => {
// Prevents duplicate overlay setup
if (document.querySelector('.cnp-overlay'))
return;
// Checks browser type to either use trustedTypes or not (Google Slides requirement)
if (isFirefox)
overlay.innerHTML = html;
else
overlay.innerHTML = trustedTypes.createPolicy("forceInner", {createHTML: (to_escape) => to_escape}).createHTML(html);
document.body.appendChild(overlay);
// Position overlay to cursor coord
const overlayContent = overlay.querySelector('.cnp-overlay-content');
let overlayLeftPos = clientX + window.scrollX + (overlayContent.offsetWidth / 2);
let overlayBottomPos = clientY + window.scrollY + (overlayContent.offsetHeight / 2);
// Flip if overlay overshoots
const tooMuchRight = overlayLeftPos + (overlayContent.offsetWidth / 2);
const tooMuchBottom = overlayBottomPos + (overlayContent.offsetHeight / 2);
if (tooMuchRight >= innerWidth)
overlayLeftPos -= overlayContent.offsetWidth;
if (tooMuchBottom >= innerHeight)
overlayBottomPos -= overlayContent.offsetHeight;
overlayContent.style.left = overlayLeftPos + 'px';
overlayContent.style.top = overlayBottomPos + 'px';
// Follow attributes of original input element
const overlayFileInput = overlay.querySelector('#cnp-overlay-file-input');
overlayFileInput.multiple = originalInput.multiple;
overlayFileInput.webkitdirectory = originalInput.webkitdirectory;
if (overlayFileInput.multiple)
overlay.querySelector('#cnp-upload-text').textContent = "Upload Files";
// Overlay handle file input
overlayFileInput.setAttribute('accept', originalInput.getAttribute('accept'));
overlayFileInput.oncancel = () => closeOverlay();
overlayFileInput.onchange = event => {
const fileList = new DataTransfer();
// Reattach previous files and append new ones
[...originalInput.files, ...event.target.files].forEach(file => fileList.items.add(file));
originalInput.files = fileList.files;
triggerChangeEvent(originalInput);
closeOverlay();
}
// Overlay upload click listener
const uploadBtn = overlay.querySelector('#cnp-upload-btn');
uploadBtn.onclick = () => overlayFileInput.click();
// Close overlay when clicked outside
if (!document.cnpRemoveListener)
document.addEventListener('click', event => {
document.cnpRemoveListener = true;
document.querySelectorAll('.cnp-overlay-content').forEach(overlayContent => {
if (!overlayContent.contains(event.target))
closeOverlay();
})
})
// Handle dragover event
const CNP_dropText = overlay.querySelector('#cnp-drop-text');
overlay.ondragover = event => {
event.stopPropagation();
event.preventDefault();
CNP_dropText.style.display = 'flex';
};
// Handle dragleave event
overlay.ondragleave = event => {
if (!overlay.contains(event.relatedTarget))
CNP_dropText.style.display = 'none';
};
// Handle drop event
overlay.ondrop = event => {
event.preventDefault();
CNP_dropText.style.display = 'none';
const fileList = new DataTransfer();
// Reattach previous files and append new ones
const excludedFolders = [...event.dataTransfer.files].filter(file => !(file.size === 0 && file.type === ''));
[...originalInput.files, ...excludedFolders].forEach(file => fileList.items.add(file));
originalInput.files = fileList.files;
triggerChangeEvent(originalInput);
closeOverlay();
};
// Handle Ctrl+V action
document.addEventListener('keydown', ctrlV);
// Handle paste event
var isPasteListenerTriggered = false;
document.addEventListener('paste', async event => {
isPasteListenerTriggered = true;
event.stopPropagation();
event.preventDefault();
const statusMap = new Map();
statusMap.set('success', 0);
statusMap.set('fail', 0);
if (overlayFileInput) {
// Access clipboard to display latest copied files to overlay
const dataTransfer = event.clipboardData;
if (dataTransfer.files.length > 0) {
ctrlVdata = cloneEvent(event.clipboardData); // Separate paste listener using Ctrl+V
// Function to handle the FileReader asynchronously
const processFiles = file => {
return new Promise(resolve => {
reader = new FileReader();
reader.onload = readerEvent => {
let webCopiedImgSrc = '';
previewImage(webCopiedImgSrc, readerEvent, file, overlay.id);
statusMap.set('success', statusMap.get('success') + 1);
resolve();
};
reader.onerror = () => {
statusMap.set('fail', statusMap.get('fail') + 1);
resolve();
};
reader.onabort = () => {return};
reader.readAsArrayBuffer(file);
});
};
// Reattach previous files for multi-file (continues in imagePreviewContainer.onclick below)
const fileList = new DataTransfer();
[...originalInput.files].forEach(file => fileList.items.add(file));
// Array of promises to process each file
const excludedFolders = [...dataTransfer.files].filter(file => !(file.size === 0 && file.type === ''));
if (excludedFolders.length == 0) {
noImage();
return;
}
else {
const badge = document.querySelector('.cnp-preview-badge');
var isFirstFile = true;
const readPromises = [...dataTransfer.files]
.filter(file => !(file.size === 0 && file.type === ''))
.map(file => {
let filename = file.name;
if (!filename || filename == 'image.png')
filename = 'CnP_'+new Date().toLocaleString('en-GB', {hour12: false}).replace(/, /g, '_').replace(/[\/: ]/g, '')+'.'+file.type.split('/').pop();
// Badge counter setup
badge.title += filename + '\n';
badge.innerText = parseInt(badge.innerText) + 1;
if (parseInt(badge.innerText) > 1)
badge.style.display = 'inline-block';
fileList.items.add(new File([file], filename, {type: file.type, lastModified: file.lastModified}));
if (isFirstFile) {
isFirstFile = false;
return processFiles(file);
}
});
await Promise.all(readPromises);
}
if (statusMap.get('fail') >= 1 && statusMap.get('success') <= 0)
noImage();
else {
const imagePreviewContainer = document.querySelector('#cnp-preview-container');
imagePreviewContainer.style.cursor = 'pointer';
imagePreviewContainer.onclick = () => {
originalInput.files = fileList.files;
triggerChangeEvent(originalInput);
closeOverlay();
};
}
} else
noImage();
}
}, { once: true, capture: true });
// Trigger paste event
overlay.contentEditable = true;
overlay.focus();
document.execCommand('paste');
if (!isPasteListenerTriggered)
window.top.postMessage({'Type': 'paste'}, '*');
isPasteListenerTriggered = false;
overlay.contentEditable = false;
// Trigger paste event for iframe
if (document.head.querySelector('script:is([id*="CnP-mutatedIframe"], [id*="CnP-iframe"])'))
window.top.postMessage({'Type': 'paste', 'iframe': document.head.querySelector('script:is([id*="CnP-mutatedIframe"], [id*="CnP-iframe"])').getAttribute('id')}, '*');
});
}
});
} catch (error) {logging(error)}
}
// Preview 'No image' message
function noImage() {
const CNP_notImage = document.createElement('span');
CNP_notImage.id = 'cnp-not-image';
CNP_notImage.textContent = 'Screenshot / Copy / Drop files';
const overlay = document.querySelector('.cnp-overlay');
const imagePreviewContainer = overlay.querySelector('#cnp-preview-container');
imagePreviewContainer.style.pointerEvents = 'none';
imagePreviewContainer.appendChild(CNP_notImage);
const spinner = document.querySelector('.cnp-spinner');
spinner.style.display = 'none';
const badge = document.querySelector('.cnp-preview-badge');
badge.style.display = 'none';
}
// Clones event objects
function cloneEvent(e) {
if (e===undefined || e===null)
return undefined;
function ClonedEvent() {};
let clone=new ClonedEvent();
for (let p in e) {
let d=Object.getOwnPropertyDescriptor(e, p);
if (d && (d.get || d.set))
Object.defineProperty(clone, p, d);
else
clone[p] = e[p];
}
Object.setPrototypeOf(clone, e);
return clone;
}
// Trigger change event on original input to update value (like disabled buttons)
function triggerChangeEvent(originalInput) {
originalInput.dispatchEvent(new Event('change', { bubbles: true }));
originalInput.dispatchEvent(new Event('input', { bubbles: true }));
}
// Close overlay immediate
function closeOverlay() {
document.querySelectorAll('.cnp-overlay').forEach(overlay => overlay.remove());
document.removeEventListener('keydown', ctrlV);
URL.revokeObjectURL(currentObjectURL);
if (reader != null)
reader.abort();
}
// Console logging for errors and messages
function logging(message) {
console.log('%c📋 Copy-n-Paste:\n', 'font-weight: bold; font-size: 1.3em;', message);
window.top.postMessage(('%c📋 Copy-n-Paste:\n', 'font-weight: bold; font-size: 1.3em;', message), '*');
}