-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
113 lines (97 loc) · 2.92 KB
/
script.js
File metadata and controls
113 lines (97 loc) · 2.92 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
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
// Injeta a função de selecionar o texto
function insertScript() {
chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
try {
await chrome.scripting.insertCSS({
target: { tabId: tabs[0].id },
files: ["styles/tooltip.css"],
});
await chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
files: [ 'libs/jquery-3.7.1.min.js' ],
});
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: loadExtension,
});
} catch (err) {
console.error(`failed to bootstrap: ${err}`);
}
});
// Fecha a janela da extensão ao carrega-la
setTimeout(() => window.close(), 100);
}
// Executa a função para selecionar texto
function loadExtension() {
/**
* Personaliza o cursor do mouse
*/
function setCursor() {
const cursorPath = 'imgs/cursor.png';
const cursorUrl = chrome.runtime.getURL(cursorPath);
[...document.body.getElementsByTagName('*')].forEach(
(elem) => (elem.style.cursor = `url(${cursorUrl}), default`)
);
}
/**
* Remove o cursor do mouse
*/
function removeCursor() {
[...document.body.getElementsByTagName('*')].forEach((elem) =>
elem.style.removeProperty('cursor')
);
}
/**
* Exibe o tooltip com o texto copiado
* @param {number} posX Posição X do evento de clique
* @param {number} posY Posição Y do evento de clique
* @param {string} text Texto obtido do evento de clique
*/
function displayTooltip(posX, posY, text) {
const fadeDelay = 1000;
const fadeDuration = 3000;
const div = $('<div class="tooltip-wrapper">')
.css({
left: `${posX}px`,
top: `${posY}px`,
})
.append($(`<span class="tooltip">Copiado: ${text}</span>`))
.appendTo(document.body);
setTimeout(() => {
div.addClass('fade-out');
setTimeout(() => div.remove(), fadeDuration);
}, fadeDelay);
}
/**
* Registra os listeners
*/
function registerListeners() {
/**
* @param {MouseEvent} event
*/
function clickListener(event) {
event.preventDefault();
const target = event.target;
const text = target.innerText || target.textContent;
navigator.clipboard.writeText(text);
displayTooltip(event.pageX, event.pageY, text);
}
/**
* @param {KeyboardEvent} event
*/
function keyListener(event) {
if (event.key === 'Escape') {
removeCursor();
document.removeEventListener('click', clickListener, false);
document.removeEventListener('keydown', keyListener, false);
}
}
document.addEventListener('click', clickListener, false);
document.addEventListener('keydown', keyListener);
}
setCursor();
registerListeners();
}
// Adiciona evento de clique ao botão para selecionar texto
document.getElementById('selectText').addEventListener('click', insertScript);
insertScript();