-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
87 lines (73 loc) · 1.9 KB
/
background.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
var videoName;
var timeout;
var currentTabId;
var youtubeId;
function onGot(item) {
let count = 1;
if(item !== undefined && item !== null && Object.keys(item).length > 0){
count = item[videoName] + 1;
}
var videoStat = {
[videoName]: count
};
browser.storage.local.set(videoStat);
}
browser.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
if(changeInfo && changeInfo.status == "complete"){
let id = getYoutubeId(tab.url);
if(id === undefined){
return;
}
browser.alarms.clearAll();
console.log("Clearing alaram");
youtubeId = id;
currentTabId = tab.id;
findVideoName(youtubeId);
}
});
function findVideoName(youtubeId){
let promise = browser.storage.local.get(youtubeId);
promise.then(onYoutubeIdFound, onError);
}
function getYoutubeId(url){
if(url === undefined)
return;
let indexOfId = url.indexOf('youtube.com/watch?v=');
if(indexOfId === undefined || indexOfId < 0)
return;
return url.substring(indexOfId + 19, indexOfId + 30);
}
browser.alarms.onAlarm.addListener(function(){
console.log("Alarm !");
let tabListener = browser.tabs.get(currentTabId);
tabListener.then(incrementVideoCount, onError);
});
function setAlarm(){
console.log("Setting alarm");
browser.alarms.create(youtubeId, {
delayInMinutes: 0.1
});
}
function onYoutubeIdFound(item){
setAlarm();
}
function incrementVideoCount(tab){
videoName = tab.title;
let youtubeId = getYoutubeId(tab.url);
let youtubeIdToName = {
[youtubeId] : videoName
}
browser.storage.local.set(youtubeIdToName);
let promise = browser.storage.local.get(videoName);
promise.then(onGot, onError);
}
browser.browserAction.onClicked.addListener(handleClick);
function handleClick(){
const url = browser.extension.getURL("gui.html");
browser.tabs.create({url: url}).then(() => {
browser.history.deleteUrl({url: url});
}).catch((e) => { throw e });
}
function onError(error) {
console.log("ERROR");
}