Skip to content

Instantly share code, notes, and snippets.

@ArtOfCode-
Created March 6, 2016 21:09
Show Gist options
  • Save ArtOfCode-/9e68a0506416f6f181f1 to your computer and use it in GitHub Desktop.
Save ArtOfCode-/9e68a0506416f6f181f1 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Smokey Sidebar
// @desc Adds recent Smokey reports to the sidebar along with feedback buttons.
// @author ArtOfCode
// @version 0.5.12
// @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/posts/add_feedback",
"xhrFields": {
"withCredentials": true
},
"data": {
"post_id": id,
"feedback_type": feedback,
}
})
.done(function(data) {
callback(true, state, data);
})
.fail(function(jqXHR, textStatus, errorThrown) {
callback(false, state, 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
});
});
}
var AVERAGE_REPORT_HEIGHT = 38;
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment