forked from abcotter/WhoFundsYourFeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
63 lines (55 loc) · 2.06 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
var past_url = '';
var processVideoUrl = 'https://6827rdxni0.execute-api.us-east-1.amazonaws.com/v1/processVideoTest';
// mutation observer will invoke the clalback function
// when the specified dom elements update
function getVideoId(url) {
const youtubeRegex = /https:\/\/www\.youtube.com\/watch\?v=([-a-zA-Z0-9_]+)/;
const videoIdMatch = url.match(youtubeRegex)[1];
return videoIdMatch;
}
function getCurrentTimeStamp() {
const currentDate = new Date();
const hours = currentDate.getHours();
const minutes = currentDate.getMinutes();
const seconds = currentDate.getSeconds();
const timeStamp = hours + ":" + minutes + ":" + seconds;
const currentDayOfMonth = currentDate.getDate();
const currentMonth = currentDate.getMonth();
const currentYear = currentDate.getFullYear();
const dateTime = currentYear + "-" + (currentMonth + 1) + "-" + currentDayOfMonth + " " + timeStamp;
return dateTime;
}
async function processWatchedVideo(body) {
const response = await fetch(processVideoUrl, {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json'
}
});
console.log('Done processing video');
console.log(body);
return response.json();
};
function sendWatchEvent() {
chrome.storage.local.get(['userId'], function(result) {
var videoId = getVideoId(location.href);
var userId = result.userId;
var timeStamp = getCurrentTimeStamp();
body = {'userId': userId, 'youtubeVideoId': videoId, 'timestamp': timeStamp};
processWatchedVideo(body).then(data => {
console.log(data);
});
});
}
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (location.href != past_url) {
past_url = location.href;
if (location.href.startsWith("https://www.youtube.com/watch?v=")) {
sendWatchEvent();
}
}
});
});
observer.observe(document.documentElement, {childList: true, subtree: true});