-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontent_script.js
59 lines (49 loc) · 1.24 KB
/
content_script.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
function getSelected() {
var focused = document.activeElement;
var selected;
if (focused) {
try {
selected = focused.value.substring(focused.selectionStart, focused.selectionEnd);
} catch (err) {
}
}
if (selected === undefined) {
selected = window.getSelection().toString();
}
if (selected !== undefined && selected !== null) {
selected = selected.trim();
return selected;
}
return '';
}
var lastSelected = '';
function onSelected(e) {
if (!document.hasFocus()) {
return true;
}
var selected = getSelected();
// if (!selected || selected.length > 1000) {
// return true;
// }
if (selected !== lastSelected) {
chrome.runtime.sendMessage({
'select' : selected
});
lastSelected = selected;
// console.log("selected: " + selected);
}
}
function initContentScript() {
document.addEventListener('mouseup', onSelected, false);
window.addEventListener('load', function() {
var input = document.getElementsByTagName('INPUT');
for (var i = 0; i < input.length; i++) {
input[i].addEventListener('select', onSelected);
}
var textarea = document.getElementsByTagName('TEXTAREA');
for (var i = 0; i < textarea.length; i++) {
textarea[i].addEventListener('select', onSelected);
}
});
}
initContentScript();