-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathselection.js
44 lines (36 loc) · 1.5 KB
/
selection.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
var selectedText = "";
function getSelectionText(){
if (window.getSelection){ // all modern browsers and IE9+
selectedText = window.getSelection().toString();
}
if (document.activeElement != null && (document.activeElement.tagName === "TEXTAREA" || document.activeElement.tagName === "INPUT")){
let selectedTextInput = document.activeElement.value.substring(document.activeElement.selectionStart, document.activeElement.selectionEnd);
if (selectedTextInput != "") selectedText = selectedTextInput;
}
if (selectedText != "") sendMessage("getSelectionText", selectedText);
// send current tab url to background.js
const url = window.location.href;
sendCurrentTabUrl(url);
}
function sendCurrentTabUrl(url) {
const urlParts = url.replace('http://','').replace('https://','').split(/[/?#]/);
const domain = urlParts[0];
targetUrl = "https://www.google.com/search?q=site" + encodeURIComponent(":" + domain + " " + selectedText);
sendMessage("sendCurrentTabUrl", targetUrl);
}
function sendMessage(action, data){
browser.runtime.sendMessage({"action": action, "data": data});
}
function handleMessage(message) {
console.log(message.action);
console.log(message.data);
switch (message.action) {
case "openUrlInSameTab":
location.assign(message.data);
break;
default:
break;
}
}
browser.runtime.onMessage.addListener(handleMessage);
document.addEventListener("contextmenu", getSelectionText);