-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
186 lines (173 loc) · 5.9 KB
/
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const buttonDelay = 250; // milliseconds
const onCapture = window.location.hostname === 'capture-maximilianjg.herokuapp.com' || window.location.hostname === 'www.getcapture.org';
const unsupportedSites = [
'www.facebook.com',
'twitter.com',
'www.instagram.com',
'www.youtube.com',
];
const onUnspportedSite = unsupportedSites.includes(window.location.hostname);
let pageHasHiglights = false;
let siteIsDisabled = false;
let globalDisabled = false;
chrome.storage.sync.get(['disabledSites', 'globalDisabled'], res => {
siteIsDisabled = res.disabledSites && res.disabledSites.includes(window.location.hostname);
globalDisabled = res.globalDisabled;
if (siteIsDisabled) {
chrome.runtime.sendMessage({ type: "EXTENSION_OFF", site: window.location.hostname });
}
});
chrome.storage.onChanged.addListener(changes => {
for (const key in changes) {
if (key === 'disabledSites') {
const disabledSites = changes[key].newValue;
siteIsDisabled = disabledSites && disabledSites.includes(window.location.hostname);
if (siteIsDisabled) {
removeCaptureButtonFromDOM();
}
} else if (key === 'globalDisabled') {
globalDisabled = changes[key].newValue;
if (globalDisabled) {
removeCaptureButtonFromDOM();
}
}
}
});
const captureButton = makeCaptureButton();
const insertButton = () => {
const selection = rangy.getSelection();
selection.trim();
const range = selection.getRangeAt(0).cloneRange();
range.collapse(false);
range.insertNode(captureButton);
}
const debounce = (func, wait, immediate) => {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
const debouncedInsertButton = debounce(insertButton, buttonDelay);
const insertHiglight = (range, id, noComment) => {
const highlight = makeHighlight(id);
const contents = range.extractContents();
highlight.appendChild(contents);
range.insertNode(highlight);
if (!noComment) {
makeCommentPopup(id, popup => highlight.appendChild(popup));
}
}
const getSelectionParentElement = () => {
let parentEl = null, sel;
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount) {
parentEl = sel.getRangeAt(0).commonAncestorContainer;
if (parentEl.nodeType != 1) {
parentEl = parentEl.parentNode;
}
}
} else if ( (sel = document.selection) && sel.type != "Control") {
parentEl = sel.createRange().parentElement();
}
return parentEl;
}
const isCaptureButton = clickTarget => {
return clickTarget && (
clickTarget.id === 'capture-button'
|| clickTarget.id === 'capture-button-image'
|| clickTarget.id === 'capture-button-image-path1'
|| clickTarget.id === 'capture-button-image-path2'
);
}
const makeHighlightRed = () => {
const highlightStyle = document.getElementById('capture-extension-highligt-error');
if (highlightStyle) { return; }
const style = document.createElement('style');
style.type = 'text/css';
style.id = 'capture-extension-highligt-error';
style.innerHTML = `
*::selection {
background-color: #FFCCCB;
}
`;
document.getElementsByTagName('head')[0].appendChild(style);
}
const removeRedHighlight = () => {
const highlightStyle = document.getElementById('capture-extension-highligt-error');
if (highlightStyle) {
highlightStyle.remove();
}
}
document.addEventListener('mousedown', e => {
removeRedHighlight();
});
document.addEventListener('mouseup', e => {
if (onUnspportedSite) {
chrome.runtime.sendMessage({ type: "UNSUPPORTED_SITE", site: window.location.hostname });
}
if (!window.getSelection || siteIsDisabled || globalDisabled|| onCapture) { return; } // return if browser doesn't support selection for some reason
const selection = rangy.getSelection();
const selectionText = selection.toString();
if (selectionText && selectionText.length > 400) {
makeHighlightRed();
chrome.runtime.sendMessage({ type: "OVER_SELECT", });
return;
} else {
removeRedHighlight();
}
const range = selection.getRangeAt(0).cloneRange();
if (isCaptureButton(e.target)) { // highlight button clicked
e.stopPropagation();
removeCaptureButtonFromDOM();
if (pageHasHiglights) {
postQuote({
selectionText,
pageUrl: getPageURl(selection),
}, response => {
const { id, status } = JSON.parse(response);
if (status === 500) {
chrome.runtime.sendMessage({ type: "ERROR", });
return;
}
insertHiglight(range, id, onUnspportedSite);
selection.removeAllRanges();
});
} else {
postSource({
selectionText,
pageUrl: getPageURl(selection),
imageSrc: getArticleImage(selection),
siteName: getSiteName(selection),
title: getArticleTitle(selection),
}, response => {
const { source, quote, status } = JSON.parse(response);
if (status === 500) {
chrome.runtime.sendMessage({ type: "ERROR", });
return;
}
pageHasHiglights = true;
insertHiglight(range, quote.id, onUnspportedSite);
if (!document.getElementById('capture-tag-box') && !onUnspportedSite) {
makeTagBox(source.id);
}
selection.removeAllRanges();
});
}
} else if (selectionText && selection.anchorNode.nodeType === 3) { // text highlighted
debouncedInsertButton();
} else if (e.target && (e.target.className === 'capture-comment-popup-input' || e.target.className === 'capture-comment-popup-button')) {
// Do nothing so that comment popup doesn't get ripped from dom
} else { // any other click on the page
removeCaptureButtonFromDOM();
hideCommentPopups();
}
});