-
Notifications
You must be signed in to change notification settings - Fork 10
/
content_script.js
41 lines (41 loc) · 1.35 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
/**
* Gets the HTML of the user's selection
*/
function getSelectionHTML() {
var userSelection;
if (window.getSelection) {
// W3C Ranges
userSelection = window.getSelection ();
// Get the range:
if (userSelection.getRangeAt)
var range = userSelection.getRangeAt (0);
else {
var range = document.createRange ();
range.setStart (userSelection.anchorNode, userSelection.anchorOffset);
range.setEnd (userSelection.focusNode, userSelection.focusOffset);
}
// And the HTML:
var clonedSelection = range.cloneContents ();
var div = document.createElement ('div');
div.appendChild (clonedSelection);
return div.innerHTML;
} else if (document.selection) {
// Explorer selection, return the HTML
userSelection = document.selection.createRange ();
return userSelection.htmlText;
} else {
return '';
}
}
/**
* Listens for a request from the button in the browser.
* When it sees the getSelection request, it returns the selection HTML, as well as the URL and title of the tab.
*/
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == "getSelection"){
var selection = window.getSelectionHTML();
sendResponse({body: selection, url: window.location.href, subject: document.title});
}
else
sendResponse({}); // snub them.
});