-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbg.js
More file actions
61 lines (50 loc) · 1.83 KB
/
bg.js
File metadata and controls
61 lines (50 loc) · 1.83 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
let questionsUrl = null;
const getYoutubeVideoId = url => {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
return (match && match[7].length == 11) ? match[7] : false;
}
// this runs every time there is an update in the browser
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status === 'loading') {
// set the icon to red
chrome.browserAction.setIcon({
path: 'images/red.png'
});
questionsUrl = null;
}
if (tab.url.includes("youtube") && changeInfo.status === 'complete') {
const vidId = getYoutubeVideoId(tab.url);
// do a http get request to check if there is a querstions url for the video
let isQuestionUrl = false;
let xhr = new XMLHttpRequest();
xhr.open('GET', `https://tubetest.herokuapp.com/api/v1/youtube/${vidId}`, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
isQuestionUrl = JSON.parse(xhr.responseText);
if (isQuestionUrl) {
questionsUrl = `https://tubetest-react.herokuapp.com/questions/${vidId}`;
}
else questionsUrl = null;
// do get request to see if there are questions for that video
if (isQuestionUrl === true) {
chrome.browserAction.setIcon({ path: 'images/green.png' });
}
}
}
xhr.send();
}
});
chrome.browserAction.onClicked.addListener(tab => {
if (questionsUrl !== null) {
// open the url in a new tab
chrome.tabs.create({ url: `${questionsUrl}` });
}
else if (tab.url.includes("youtube.com") && getYoutubeVideoId(tab.url) !== false) {
// send user to create a question link
let vidId = getYoutubeVideoId(tab.url);
chrome.tabs.create({
url: `https://tubetest-react.herokuapp.com/question/${vidId}`
});
}
});