-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsebridge.js
More file actions
146 lines (133 loc) · 4.47 KB
/
sebridge.js
File metadata and controls
146 lines (133 loc) · 4.47 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const storageUUID = crypto.randomUUID();
var SEBridge = `
function IsInOBS() {
return (typeof(window.obsstudio) !== 'undefined');
}
if (!IsInOBS()) {
let slime2CSS = document.createElement("link");
slime2CSS.setAttribute("href", "https://cdn.jsdelivr.net/gh/zaytri/slime2@latest/release/slime2.css");
slime2CSS.setAttribute("rel", "stylesheet");
document.head.appendChild(slime2CSS);
}
const SE_API = {
store: {
set: function(keyName, obj) {
if (obj === null)
slime2.storage.del(keyName);
else
slime2.storage.set(keyName, obj);
},
get: function(keyName) {
return slime2.storage.get(keyName);
}
},
cheerFilter: async function(msg) {
return new Promise((resolve, reject) => {
let ret = "";
if (typeof msg === 'string')
ret = msg.replace(/cheer[0-9]+/gm, "");
resolve(ret);
});
},
getOverlayStatus: function() {
const isOBS = (typeof(window.obsstudio) !== 'undefined');
return {"isEditorMode": isOBS, "muted": false};
},
resumeQueue: function() { },
sanitize: async function(msgObj) {
return new Promise((resolve, reject) => {
if (Object.hasOwn(msgObj, "message"))
resolve({"result": {"message": msgObj.message}, "skip": false});
else
reject();
});
},
counters: {
get: async function(counterName) {
return new Promise(async (resolve, reject) => {
const counterValue = await SE_API.store.get(counterName);
resolve({"counter": counterName, "value": counterValue});
});
}
},
};
function seEmotesData(emote) {
this.type = emote.source;
this.name = emote.name;
this.id = emote.id;
this.gif = false;
this.start = -1;
this.end = -1;
this.urls = {"1": emote["images"]["default"]["x1"], "2": emote["images"]["default"]["x2"], "4": emote["images"]["default"]["x4"]};
}
function seEventData(data, message, isAction) {
const messageDefined = (message !== undefined);
if (messageDefined && message.user !== undefined && message.user != null) {
let user = message.user;
this.nick = user.userName;
this.displayName = user.displayName;
this.pronouns = user.pronouns;
this.badges = user.badges;
// map the objects to the same properties
this.badges.forEach((badge, index) => {
this.badges[index].url = this.badges[index].image;
this.badges[index].id = this.badges[index].type;
});
}
this.msgId = data.id;
this.userId = data.userId;
this.isAction = isAction;
if (messageDefined && message != null) {
this.text = message.text;
this.tags = message.tags;
// Attempt to create the emote parts
this.emotes = new Array();
for (const part of message.parts) {
if (part.type === "emote") {
this.emotes.push(new seEmotesData(part.emote));
}
}
}
}
// wrapper for the bridge event, the event data is most of the work.
function seBridgeEvent(data) {
let isAction = false;
this.listener = data.type;
// Handle message types and translations
if (data.type === "remove-message")
this.listener = "delete-message";
else if (data.type === "clear-messages") {
// Refresh widget on clear command
document.location.reload();
}
else if (data.type === "remove-user")
this.listener = "delete-messages";
else if (data.type === "action") {
this.listener = "message";
isAction = true;
}
else if (data.type === "reply" || data.type === "basic" || data.type === "announcement" || data.type === "highlight" || data.type === "resub" || data.type === "cheer" || data.type === "click")
this.listener = "message";
this.event = {"data": new seEventData(data, data.message, isAction)};
}
function seSettings() {
// Some old old systems in streamelements used to push channel usernames
// don't think these are really grabbable in slime2
this.channel = {"username":"", "id":0};
this.currency = {"symbol":""};
this.fieldData = seJsonConfig;
}
// Call the slime
addEventListener("slime2:ready", () => {
slime2.widget.loadPlatform("twitch");
slime2.storage.use("${storageUUID}");
// connect to our bridge and refire as necessary
slime2.onEvent(ev => {
const event = new CustomEvent("onEventReceived", { bubbles: true, cancelable: false, detail: new seBridgeEvent(ev)});
window.dispatchEvent(event);
});
// Fire the event for streamelement listeners on startup.
const loadedEvent = new CustomEvent("onWidgetLoad", {bubbles: true, cancelable: false, detail: new seSettings()});
window.dispatchEvent(loadedEvent);
});
`;