forked from joker314/ScratchAccountManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
85 lines (65 loc) · 2.73 KB
/
code.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
chrome.storage.sync.get(null, users => {
const dropdown = document.querySelector(".dropdown-menu > ul, .dropdown.production");
if(dropdown) {
// User is logged in.
const username = dropdown.firstChild.firstChild.href.replace(/.+\/([a-zA-Z0-9_-]+)\/$/, "$1");
const icon = document.querySelector(".user-icon, .user-info > img").src;
const truncated = document.querySelector(".user-icon, .user-info > img").nextSibling.textContent;
const markAsAlt = document.createElement("LI");
let amIAnAlt = Object.keys(users).includes(username);
markAsAlt.appendChild(
document.createTextNode(amIAnAlt ? "Unmark as alt" : "Mark as alt")
);
markAsAlt.addEventListener("click", function(e){
if(amIAnAlt) {
markAsAlt.firstChild.textContent = "Mark as alt";
chrome.storage.sync.remove(username);
} else {
markAsAlt.firstChild.textContent = "Unmark as alt";
chrome.runtime.sendMessage({getCookies: true}, function(sid) {
let entry = {};
entry[username] = {icon, sid, truncated, count: 0};
chrome.storage.sync.set(entry);
Object.assign(users, entry);
});
}
amIAnAlt = !amIAnAlt;
});
Object.keys(users).filter(user => user !== username).forEach(function(user) {
const altAccount = document.createElement("A");
const outer = document.createElement("LI");
outer.appendChild(altAccount);
dropdown.appendChild(outer);
const altIcon = document.createElement("IMG");
const altUsername = document.createTextNode(users[user].truncated); // Use truncated username
const altMessages = document.createElement("SPAN"); // Message count
altMessages.className = "msg-count";
altMessages.id = "alt-account-user-" + user;
if(users[user].count) {
altMessages.textContent = users[user].count;
altMessages.className = "msg-count visible";
} else {
altMessages.className = "msg-count";
}
altIcon.src = users[user].icon;
altIcon.className = "avatar user-icon";
altAccount.appendChild(altIcon);
altAccount.appendChild(altUsername);
altAccount.appendChild(altMessages);
altAccount.addEventListener("click", function(e) {
chrome.runtime.sendMessage({setCookies: true, sid: users[user].sid}, function() {
if(e.path[0] === altMessages && location.pathname !== "/messages/") {
location.href = "https://scratch.mit.edu/messages/";
} else {
location.reload();
}
});
});
});
dropdown.appendChild(markAsAlt);
chrome.runtime.onMessage.addListener(function(req) {
document.querySelector("#alt-account-user-" + req.name).textContent = req.count;
document.querySelector("#alt-account-user-" + req.name).className = req.count ? "msg-count visible" : "msg-count";
});
}
});