forked from makakiyoAnju/streamlabs-custom-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
127 lines (106 loc) · 4.7 KB
/
functions.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Variables and constructs
const boardId = '{Your UserID\BoardcasterID}';
// URLs to use
const defaultAvatarUrl = `{default_avatar}`;
// I wouldn't touch this unless you know what you are doing
const endpoint = channelName => `https://api.twitch.tv/helix/users?login=${channelName}`;
// Saved information
const clientId = '{Your Client ID here}'; // Only need to edit if you are doing your own Twitch app registry
var accessToken = '{Your OAuth token here}'; // Only change if you need a new token
const cache = {};
// Adds and changes font
var fontSel = document.getElementById("fontSel").value;
if (fontSel !== "optionA") {
document.body.style.fontFamily = `'{fontPicker}'`;
WebFont.load({
google: {
families: [`{fontPicker}`]
}
});
} else {
document.body.style.fontFamily = 'Visby Round CF Bold';
}
document.addEventListener('onLoad', function(obj) {
// obj will be empty for chat widget
// this will fire only once when the widget loads
});
document.addEventListener('onEventReceived', function(obj) {
if (!obj || typeof obj.detail === 'undefined' || obj.detail === null) { return; }
const { from: username, messageId, tags, userid } = obj.detail;
const displayName = tags['display-name'];
const userId = tags['user-id'];
// Display in the console log (Chrome - Right click anywhere > Inspect > Console)
console.log(`${displayName}`, '<-');
if (!username) { return; }
// Caches the Twitch user to be used somewhere else eventually
/*if (typeof cache[displayName] !== 'undefined') {
const elems = displayName !== null ? document.getElementsByClassName(`message-${displayName}-avatar`) : document.getElementsByClassName('message--avatar');
for (const elem of elems) {
elem.src = cache[username];
}
return;
}*/
// Fetches data of the Twitch user profile pic
fetch(endpoint(username), {
"method": 'GET',
"headers": {
'Client-ID': clientId,
'Authorization': "Bearer " + accessToken
}
}).then(r => {
if (r.status < 200 || r.status > 299) {
cache[username] = defaultAvatarUrl;
return;
}
return r.json();
}).then(({ data }) => {
const [ user ] = data;
cache[username] = user['profile_image_url'];
}).catch(() => {
cache[username] = defaultAvatarUrl;
}).finally(() => {
const elems = displayName !== null ? document.getElementsByClassName(`message-${displayName}-avatar`) : document.getElementsByClassName('message--avatar');
for (const elem of elems) {
elem.src = cache[username];
}
});
// Switches the alignment of the default messages
const subElems = displayName !== null ? document.getElementsByClassName(`${displayName}-id`) : document.getElementsByClassName('-id');
for (const subElem of subElems) {
if (document.getElementById("defaultAlign").value == "defaultRight") {
subElem.querySelector(".chat").classList.add('bubbleRight');
subElem.classList.add('justifyRight');
$(document).ready(function() { $(`.message-${displayName}-avatar`).each(function() { $(this).insertAfter($(this).parent().find('.message')); }); });
} else {
subElem.querySelector(".chat").classList.add('bubbleLeft');
}
}
// Check if the user is subscribed to the channel
fetch(`https://api.twitch.tv/helix/subscriptions?broadcaster_id=${boardId}&user_id=${userId}`, {
"method": 'GET',
"headers": {
'Client-ID': clientId,
'Authorization': "Bearer " + accessToken
}
}).then(s => {
return s.json();
}).then(({ data }) => {
const [ subUser ] = data;
const subTier = subUser['tier'];
const subTierClass = subTier == 3000 ? 'tier3' : subTier == 2000 ? 'tier2' : 'tier1';
const subElems = displayName !== null ? document.getElementsByClassName(`${displayName}-id`) : document.getElementsByClassName('-id');
for (const subElem of subElems) {
subElem.querySelector(".chat").classList.replace('tier', subTierClass);
if (document.getElementById("subscriberAlign").value == "subRight") {
subElem.querySelector(".chat").classList.replace('bubbleLeft', 'bubbleRight');
subElem.classList.add('justifyRight');
$(document).ready(function() { $(`.message-${displayName}-avatar`).each(function() { $(this).insertAfter($(this).parent().find('.message')); }); });
} else {
subElem.querySelector(".chat").classList.replace('bubbleRight', 'bubbleLeft');
subElem.classList.remove('justifyRight');
$(document).ready(function() { $(`.message-${displayName}-avatar`).each(function() { $(this).insertBefore($(this).parent().find('.message')); }); });
}
//console.log(`${displayName}`, `${subTierClass}`);
}
}).catch((error) => { return; });
});