-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstrawpollwidget.user.js
147 lines (118 loc) · 3.73 KB
/
strawpollwidget.user.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
// ==UserScript==
// @name Strawpoll Box
// @namespace http://tampermonkey.net/
// @version 0.2.4
// @description Adds a list of recent polls that can be previewed and voted on!
// @author The Flamin' Taco
// @include *://chat.meta.stackoverflow.com/rooms/*
// @include *://chat.meta.stackexchange.com/rooms/*
// @include *://chat.stackexchange.com/rooms/*
// @include *://chat.stackoverflow.com/rooms/*
// @include *://chat.askubuntu.com/rooms/*
// @include *://chat.serverfault.com/rooms/*
// @run-at document-end
// @grant none
// ==/UserScript==
/* global $ */
(function() {
'use strict';
// This horrifying one liner adds the polls widget.
$($("#widgets").find("div.sidebar-widget")[1]).after($(`<div class="sidebar-widget" style="display:block;"><div class="fr msg-small">Recent polls <a id="displayPollsButton" onclick="toggle_polls()" class="fake_link">show</a></div><br class="cboth"><ul id="polls" class="collapsible" style="display:none;"></ul></div>`));
$("body").append(`<style>.fake_link:hover{
cursor: pointer;
}
.scores{
font-size: 10px;
position: relative;
left: 32px;
}
.poll{
font-size:10px;
}
.scores tr{
border-bottom:solid 1px black;
border-bottom:solid 1px rgba(0,0,0,0.3);
}
.poll_option{
font-weight:bold;
}
.poll_score{
padding-left:12px;
}</style>`);
var polls = $("#polls");
var socket;
var room = CHAT.CURRENT_ROOM_ID;
var polls_data = [];
var updatePolls = function(){
polls.html(""); // Clear the current polls.
polls_data.forEach((poll,i)=>{
var entry = document.createElement('li');
entry.setAttribute("id",`poll-${i}`);
entry.setAttribute('class', 'poll');
entry.setAttribute('style','display:block;');
var pollLink = document.createElement('a');
pollLink.setAttribute('href',`http://www.strawpoll.me/${poll.poll_id}`);
pollLink.innerHTML = `${poll.title || poll.poll_id}`
entry.append(pollLink);
entry.append(' - ');
var permalink = document.createElement("a");
permalink.setAttribute('class', 'permalink');
permalink.setAttribute('rel', 'noreferrer noopener')
permalink.setAttribute('href', `/transcript/message/${poll.msg_id}#${poll.msg_id}`);
permalink.textContent = `${timeSpanString(Math.floor(new Date()/1000)-poll.time)} ago`;
entry.append(permalink);
entry.append(' by ');
var user = document.createElement('a');
user.setAttribute('href', `/users/${poll.user}`);
user.textContent = poll.user_name;
entry.append(user);
var scores = document.createElement("table");
scores.setAttribute("class", "scores");
for(var i=0; i<poll.options.length; i++){
var score = document.createElement('tr');
var optionName = document.createElement('td');
optionName.setAttribute("class", "poll_option");
optionName.innerHTML = poll.options[i];
score.append(optionName);
var vote = document.createElement("td");
vote.setAttribute("class", "poll_score");
vote.append(poll.votes[i]);
score.append(vote);
scores.append(score);
}
entry.append(scores);
polls.append(entry);
});
}
var onmessage = function(msg){
try{
polls_data = JSON.parse(msg.data);
updatePolls();
}catch(e){
console.error(e);
console.log(msg);
}
return;
}
var connect = function(){
socket = new WebSocket("wss://a-ta.co", "pollbox");
socket.onmessage = onmessage;
socket.onclose = connect;
}
connect();
var pollsOn = false;
window.toggle_polls = function(){
if(pollsOn){
polls.css({display: "none"});
$("#displayPollsButton").text("show");
}else{
polls.css({display: "block"});
$("#displayPollsButton").text("hide");
}
pollsOn = !pollsOn;
return false;
}
setInterval(function(){
socket.send(JSON.stringify({action: "update", data: room}))
}, 5 * 1000);
})();