-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
52 lines (45 loc) · 1.59 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
var discordUserID = '0000000000';
//Get data from Chrome synced storage
const getStorageData = key =>
new Promise((resolve, reject) =>
chrome.storage.sync.get(key, result =>
chrome.runtime.lastError
? reject(Error(chrome.runtime.lastError.message))
: setDiscordUserID(result)
)
)
//Set discordUserID to default state in Chrome storage
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.set({ discordUserID });
});
//Trigger an event when a tab is loaded
chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete' && tab.url.startsWith("https://www.youtube.com/watch")) {
console.log("YouTube tab loaded! ID: " + discordUserID)
const data = { id: discordUserID };
//Send our ID to the bot server to deduct a DiscordDollar
fetch('https://harvbot.jjwebb.repl.co/users/15', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
registration.showNotification("Discordbot: ", {
body: "Spent a DiscordDollar to watch YouTube!",
icon: "images/Discord/discord128.png",
})
})
.catch((error) => {
console.error('Error:', error);
});
console.log(tab.url)
}
})
function setDiscordUserID(id) {
discordUserID = id.discordUserID;
}
getStorageData("discordUserID")