-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-script.js
73 lines (67 loc) · 2.47 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
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
// console.log('Content script injected!');
window.regex = new RegExp("^https:\/\/.*facebook\.com\/.*/videos\/\(.*\)\/.*", "g");
window.regexAlt = new RegExp("^https:\/\/.*facebook\.com\/.*/videos\/.*\/\(.*\)\/.*", "g");
window.videoIds = [];
(function init() {
// create an observer instance
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
const observer = new MutationObserver(function(mutations) {
mutations.forEach(mutation => { // Iterate through all mutation records
for(let i = 0; i < mutation.addedNodes.length; i++) {
maybeAddDownloadButtons(mutation.addedNodes[i]);
}
});
});
const target = document.getElementById('contentArea'); // select the target node
if(target) {
maybeAddDownloadButtons(target);
observer.observe(target, { attributes: true, childList: true, subtree: true });
}
})();
function maybeAddDownloadButtons(target) {
if (target && target.querySelectorAll) {
const elements = target.querySelectorAll('[href*="/videos/"]');
if (elements) {
[].forEach.call(elements, function (element) {
let videoId, btnStyle, referenceElement;
if (element.href.includes('?comment')) {
return;
}
videoId = element.href.replace(window.regex, '$1');
btnStyle = 'background-color: pink';
referenceElement = element;
if(isNaN(videoId)) { // Invalid video ID
// Special cases:
// videoId.includes('vb.') || videoId.includes('vl.') || videoId.includes('pcb.')
videoId = element.href.replace(window.regexAlt, '$1'); // Try one more time
if (isNaN(videoId)) return;
btnStyle = 'background-color: pink; z-index: 2; position: absolute';
referenceElement = element.firstChild;
}
if(!window.videoIds.includes(videoId)) {
// Create a download button
const btn = document.createElement("BUTTON");
btn.innerHTML = "Download this";
btn.setAttribute('style', btnStyle);
btn.setAttribute('value', videoId);
btn.setAttribute('class', 'downloadButton');
btn.addEventListener('click', handleClick);
referenceElement.parentNode.insertBefore(btn, referenceElement);
window.videoIds.push(videoId);
}
});
}
}
}
function handleClick(ev) {
ev.preventDefault();
ev.stopPropagation();
// console.log('Downloading', ev.target.value);
const data = {
messageType: "download-video",
videoId: ev.target.value
}
chrome.runtime.sendMessage(data, function(response) {
console.log(response.answer);
});
}