-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
29 lines (26 loc) · 868 Bytes
/
content.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
function getSelectionText()
{
var text = "";
var activeEl = document.activeElement;
var activeElTagName = activeEl ? activeEl.tagName.toLowerCase() : null;
if ( (activeElTagName == "textarea") || (activeElTagName == "input" &&
/^(?:text|search|password|tel|url)$/i.test(activeEl.type)) &&
(typeof activeEl.selectionStart == "number"))
{
text = activeEl.value.slice(activeEl.selectionStart, activeEl.selectionEnd);
}
else if (window.getSelection)
{
text = window.getSelection().toString();
}
return text;
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse){
if(request.message ==="clicked_browser_action"){
var text = getSelectionText();
console.log(text);
chrome.runtime.sendMessage({"message": "open_new_tab", "url": text});
}
}
);