-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaying.js
75 lines (61 loc) · 2 KB
/
playing.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
let extensionID = "agnaejlkbiiggajjmnpmeheigkflbnoo"; //Chrome
if (typeof browser !== 'undefined' && typeof chrome !== "undefined"){
extensionID = "{57081fef-67b4-482f-bcb0-69296e63ec4f}"; //Firefox
}
let clientID = "1319571957867352165";
chrome.runtime.sendMessage(extensionID, {mode: 'passive'}, function(response) {
console.log('Presence registred', response);
});
chrome.runtime.onMessage.addListener(function(info, sender, sendResponse) {
console.log('Presence requested', info);
sendResponse(generatePresence());
});
//Setting the date here so that it does not get reset when presence is re-requested
let time = Date.now();
let windowName = "";
let projectName = "";
// Get the tab's name once everything is loaded so it isn't the default (Scratch - Imagine, Program, Share)
window.onload = () => {
// Add a little delay
setTimeout(() => {
windowName = document.title;
// The project name without the " on Scratch" (11 characters)
projectName = windowName.substring(0, windowName.length - 11);
console.log(projectName);
}, 10000);
/*
The 10 second delay is because it seems that window.onload is fired just a bit before the tab name is changed
to the actual project name. The duration of 10 seconds is not signficant, as the presence is requested every 15 seconds
*/
}
function generatePresence() {
//Check if the windowName is not empty
if (windowName === "") {
return (
{
clientId: clientID,
presence: {
details: "Playing a Scratch project!",
startTimestamp: time,
largeImageText: "Scratch",
instance: true
}
}
);
}
else { // Else we will display the project we are working on
return (
{
clientId: clientID,
presence: {
details: `Playing - ${projectName}`,
startTimestamp: time,
largeImageKey: "icon_48",
largeImageText: "Scratch",
smallImageKey: "playing",
instance: true
}
}
);
}
}