forked from Charcoal-SE/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smokey_sidebar.user.js
139 lines (130 loc) · 5.43 KB
/
smokey_sidebar.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
// ==UserScript==
// @name Longer Sidebar
// @desc Makes the sidebar longer for symmetry by using recent Smokey reports.
// @author ArtOfCode
// @version 0.6.1
// @updateURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/smokey_sidebar.user.js
// @downloadURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/smokey_sidebar.user.js
// @supportURL https://github.com/Charcoal-SE/Userscripts/issues
// @grant none
// @match *://*.stackexchange.com/*
// @match *://*.stackoverflow.com/*
// @match *://*.superuser.com/*
// @match *://*.serverfault.com/*
// @match *://*.askubuntu.com/*
// @match *://*.stackapps.com/*
// @match *://*.mathoverflow.net/*
// @exclude *://chat.stackexchange.com/*
// @exclude *://chat.meta.stackexchange.com/*
// @exclude *://chat.stackoverflow.com/*
// ==/UserScript==
'use strict';
var userscript = function($) {
function getSmokeyReports(number, callback) {
var url = "https://metasmoke.erwaysoftware.com/posts/recent.json";
$.ajax({
url: url,
type: 'GET',
data: {
'size': number
}
})
.done(function(data) {
callback(true, data);
})
.error(function(jqXHR, textStatus, errorThrown) {
callback(false, jqXHR);
console.error("XMLHttpRequest to metasmoke failed. Details follow.");
console.log({
type: 'IncompleteXhrException',
message: 'The XMLHttpRequest did not complete.',
status: textStatus,
error: errorThrown,
response: jqXHR.responseText
});
});
}
function getAvailableSpace() {
return $("#mainbar").height() - $("#sidebar").height();
}
function addPostFeedback(id, feedback, state, callback) {
$.ajax({
"type": "POST",
"url": "https://metasmoke.erwaysoftware.com/api/w/post/" + id + "/feedback",
"xhrFields": {
"withCredentials": true
},
"data": {
"type": feedback,
"key": "9f2d0243d7a7fee2c3af0408aec1fe50327c428096f663a37f3e9cfbae2ef01b"
}
})
.done(function(data) {
callback(true, state, data);
})
.fail(function(jqXHR, textStatus, errorThrown) {
callback(false, state, jqXHR);
StackExchange.helpers.showErrorMessage($('.topbar'), "Error while feeding back to metasmoke.", {
'position': 'toast',
'transient': true,
'transientTimeout': 10000
});
});
}
var AVERAGE_REPORT_HEIGHT = 80;
if(getAvailableSpace() >= AVERAGE_REPORT_HEIGHT) {
getSmokeyReports(Math.floor(getAvailableSpace() / AVERAGE_REPORT_HEIGHT), function(success, data) {
var $module = $("<div class='module'></div>");
$module.append("<h3>SmokeDetector Reports</h3>");
var $list = $("<ul></ul>");
$list.css("list-style-type", "square");
if(success) {
for(var i = 0; i < data.length; i++) {
var dt = new Date(data[i].created_at);
$("<li></li>")
.append("<img style='padding-right: 5px;' src='" + data[i].site_logo + "' height='16px' width='16px' />")
.append("<a style='padding-bottom: 5px;' href='" + data[i].link + "'>" + data[i].title + "</a>")
.append(" at " + dt.toLocaleTimeString())
.append("<br/>")
.append("<a class='ms-report' href='#' data-postid='" + data[i].id + "' data-feedback='tp'>✓</a>")
.append("<a class='ms-report' href='#' data-postid='" + data[i].id + "' data-feedback='fp'>❌</a>")
.append("<a class='ms-report' href='#' data-postid='" + data[i].id + "' data-feedback='naa'><b>N</b></a>")
.appendTo($list);
}
$list.appendTo($module);
$module.appendTo("#sidebar");
}
else {
$module.append("<em>Unavailable right now - XHR incomplete, check console.</em>");
$module.appendTo("#sidebar");
}
$(".ms-report").css("margin-left", "5px").css("margin-right", "5px");
$(".ms-report").bind('click', function(e) {
e.preventDefault();
var id = $(this).data("postid");
var feedback = $(this).data("feedback");
var state = {
'id': id,
'feedback': feedback,
'parent': $(this).parent()
}
addPostFeedback(id, feedback, state, function(success, state, data) {
if(!success) {
alert("Failed to feed back: " + data.status);
}
else {
state['parent'].find(".ms-report").each(function() {
$(this).fadeOut(200, function() {
$(this).remove();
});
});
}
});
});
});
}
}
var el = document.createElement("script");
el.type = "application/javascript";
el.text = "(" + userscript + ")(jQuery);";
document.body.appendChild(el);