-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapture.js
More file actions
69 lines (60 loc) · 1.95 KB
/
Copy pathcapture.js
File metadata and controls
69 lines (60 loc) · 1.95 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(function () {
class Capture {
constructor() {
this.window = window;
this.document = document;
this.location = location;
this.selection_text = escapeIt(window.getSelection().toString());
if (!this.selection_text) {
this.selection_text = this.getMainText();
}
if (this.debug) {
logTEXT(this.selection_text);
}
this.encoded_url = encodeURIComponent(location.href);
this.escaped_title = escapeIt(document.title);
}
capture() {
chrome.runtime.sendMessage({
"action": "summarizeContent",
"content": this.selection_text,
"url": this.encoded_url,
"title": this.escaped_title,
});
}
captureIt(options) {
if (chrome.runtime.lastError) {
alert(
"Could not capture url. Error loading options: " +
chrome.runtime.lastError.message
);
return;
}
for (var k in options) this[k] = options[k];
this.capture();
}
getMainText() {
var documentClone = this.document.cloneNode(true);
var article = new Readability(documentClone).parse();
return article.textContent;
}
}
function escapeIt(text) {
return encodeURIComponent(text)
.replace(/\(/g, "%28") // Escape '('
.replace(/\)/g, "%29") // Escape ')'
.replace(/'/g, "%27"); // Escape "'"
}
function logTEXT(text) {
window.console.log(
"Capturing the following URI with new org-protocol: ",
text
);
return text;
}
var capture = new Capture();
var f = function (options) {
capture.captureIt(options);
};
chrome.storage.sync.get(null, f);
})();