-
Notifications
You must be signed in to change notification settings - Fork 1
/
owncast.js
112 lines (101 loc) · 3.01 KB
/
owncast.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
(function () {
function toDataURL(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function() {
callback(reader.result);
}
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
}
function processMessage(ele, wss=true){
if(ele.hasAttribute("is-deleted")) {
return;
}
var chatname = "";
try{
chatname = ele.querySelector(".message-author").innerText;
} catch(e){}
var chatmessage = "";
try{
chatmessage = ele.querySelector(".message-text").innerHTML;
} catch(e){}
if (textOnlyMode){
chatmessage = ele.querySelector(".message-text").innerText;
}
var chatimg = "";
var chatdonation = "";
var chatmembership = "";
var chatsticker = "";
var chatbadges = "";
var hasDonation = '';
var hasMembership = '';
var data = {};
data.chatname = chatname;
data.chatbadges = chatbadges;
data.chatmessage = chatmessage;
data.chatimg = ""; // Doesn't seem to be an avatar image for owncast
data.hasDonation = hasDonation;
data.hasMembership = hasMembership;
data.type = "owncast";
try {
chrome.runtime.sendMessage(chrome.runtime.id, { "message": data }, function(){});
} catch(e){}
}
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
try{
if ("focusChat" == request){
document.querySelector("div#message-input").focus();
sendResponse(true);
return;
}
if ("textOnlyMode" == request){
textOnlyMode = true;
sendResponse(true);
return;
} else if ("richTextMode" == request){
textOnlyMode = false;
sendResponse(true);
return;
}
} catch(e){}
sendResponse(false);
}
);
var textOnlyMode = false;
chrome.runtime.sendMessage(chrome.runtime.id, { "getSettings": true }, function(response){ // {"state":isExtensionOn,"streamID":channel, "settings":settings}
if ("settings" in response){
if ("textonlymode" in response.settings){
textOnlyMode = response.settings.textonlymode;
}
}
}); /////
function onElementInserted(containerSelector, callback) {
var onMutationsObserved = function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length) {
for (var i = 0, len = mutation.addedNodes.length; i < len; i++) {
if (mutation.addedNodes[i] && mutation.addedNodes[i].className && mutation.addedNodes[i].classList.contains("message")) {
callback(mutation.addedNodes[i]);
}
}
}
});
};
var target = document.querySelector(containerSelector);
if (!target){return;}
var config = { childList: true, subtree: true };
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(onMutationsObserved);
observer.observe(target, config);
}
console.log("social stream injected");
onElementInserted("#messages-only", function(element){
processMessage(element, false);
});
})();