-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
38 lines (31 loc) · 1.18 KB
/
Copy pathbackground.js
File metadata and controls
38 lines (31 loc) · 1.18 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
/* The function that finds and returns the selected text */
const getSelection = function () {
let selection = window.getSelection();
return (selection.rangeCount > 0) ? selection.toString() : '';
};
/* This line converts the above function to string
* (and makes sure it will be called instantly) */
const codeString = ';(' + getSelection + ')();';
chrome.commands.onCommand.addListener(function(cmd){
if(cmd === 'selectedText') {
chrome.tabs.executeScript({
code: codeString,
allFrames: true
},
function (selectedTextPerFrame) {
if (chrome.runtime.lastError) {
alert('ERROR:\n' + chrome.runtime.lastError.message);
}
else if ((selectedTextPerFrame.length > 0)
&& (typeof(selectedTextPerFrame[0]) === 'string')) {
let selectedText = selectedTextPerFrame[0];
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
let activeTab = tabs[0];
let activeTabUrl = activeTab.url;
chrome.storage.sync.set({selectedText : activeTabUrl}, function(result) {
alert(selectedTextPerFrame[0]);
});
});
}
})
}});