forked from aaronpk/live-chat-overlay
-
Notifications
You must be signed in to change notification settings - Fork 14
/
polleverywhere.js
195 lines (169 loc) · 5.08 KB
/
polleverywhere.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
(function() {
var soca=false;
function generateStreamID(){
var text = "";
var possible = "ABCEFGHJKLMNPQRSTUVWXYZabcefghijkmnpqrstuvwxyz23456789";
for (var i = 0; i < 11; i++){
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
var channel = generateStreamID();
var outputCounter = 0; // used to avoid doubling up on old messages if lag or whatever
var sendProperties = ["color","scale","sizeOffset","commentBottom","commentHeight","authorBackgroundColor","authorAvatarBorderColor","authorColor","commentBackgroundColor","commentColor","fontFamily","showOnlyFirstName","highlightWords"];
var connecting = false;
function actionwtf(){ // steves personal socket server service
if (soca){return;}
soca = new WebSocket("wss://api.overlay.ninja");
soca.onclose = function (){
console.error("closed: reconnecting");
clearTimeout(connecting);
connecting = setTimeout(function(){soca=false;actionwtf(); },2000);
};
soca.onopen = function (){
try {
soca.send(JSON.stringify({"join":channel}));
console.log("connected");
} catch(e){
console.error(e);
soca.close();
clearTimeout(connecting);
connecting = setTimeout(function(){soca=false;actionwtf(); },2000);
}
};
soca.onerror = function (e){
console.error(e);
soca.close();
clearTimeout(connecting);
connecting = setTimeout(function(){soca=false;actionwtf(); },2000);
};
chrome.storage.sync.set({
streamID: channel
});
}
function pushMessage(data){
var message = {};
message.msg = true;
message.contents = data;
console.log(message);
try {
chrome.storage.sync.get(sendProperties, function(item){
outputCounter+=1;
message.id = outputCounter;
message.settings = item;
soca.send(JSON.stringify(message));
});
} catch(e){
outputCounter+=1;
message.id = outputCounter;
soca.send(JSON.stringify(message));
}
}
var showOnlyFirstName;
var highlightWords = [];
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();
}
NodeList.prototype.forEach = Array.prototype.forEach
function prepMessage(ele){
if (ele == window){return;}
console.log(ele);
var img = false;
var chatimg = "";
if (!chatimg){
chatimg = "https://chat.overlay.ninja/polleverywhere.png";
}
var name = "";
try{
name = ele.querySelector(".participant").innerText;
if (name){
name = name.replace("User: ","");
name = name.trim();
}
} catch(e){}
console.log(name);
var msg = "";
try {
msg = ele.querySelector('.value').innerText;
} catch(e){
}
if (msg){
msg = msg.trim();
}
var data = {};
data.chatname = name;
data.chatbadges = "";
data.backgroundColor = "";
data.textColor = "";
data.chatmessage = msg;
data.chatimg = chatimg;
data.hasDonation = "";
data.hasMembership = "";;
data.contentimg = "";
data.type = "polleverywhere";
pushMessage(data);
}
var properties = ["color","scale","streamID","sizeOffset","commentBottom","commentHeight","authorBackgroundColor","authorAvatarBorderColor","authorColor","commentBackgroundColor","commentColor","fontFamily","showOnlyFirstName","highlightWords"];
chrome.storage.sync.get(properties, function(item){
var color = "#000";
if(item.color) {
color = item.color;
}
if (item.streamID){
channel = item.streamID;
} else {
chrome.storage.sync.set({
streamID: channel
});
}
});
function startup() {
console.log("STARTED");
setTimeout(function(){actionwtf();},10);
setInterval(function(){
try {
var main = document.querySelectorAll(".discourse--result--content");
if (!main){return;}
console.log(main);
for (var j =0;j<main.length;j++){
try{
if (!main[j].dataset.set){
main[j].dataset.set = "true";
main[j].insertAdjacentHTML('afterbegin', '<div><span><a class="btn-push-pollev" style="margin-right:10px;color:green;font-weight:700;">ADD</a></span><span><a class="btn-clear-pollev" style="margin-right:10px;color:red;">CLEAR</a></span><span><a class="btn-getoverlay-pollev" >LINK</a></span></div>');
main[j].querySelector(".btn-push-pollev").onclick = function(event){
event.preventDefault();
event.stopPropagation();
prepMessage(this.parentNode.parentNode.parentNode.parentNode);
return false;
};
main[j].querySelector(".btn-getoverlay-pollev").onclick = function(event){
event.preventDefault();
event.stopPropagation();
prompt("Overlay Link: https://chat.overlay.ninja?session="+channel+"\nAdd as a browser source; set height to 250px", "https://chat.overlay.ninja?session="+channel);
return false;
};
main[j].querySelector(".btn-clear-pollev").onclick = function(event){
event.preventDefault();
event.stopPropagation();
pushMessage(false);
return false;
}
}
} catch(e){}
}
} catch(e){ return; }
},2000);
}
console.log("STARTING");
startup();
})();