-
Notifications
You must be signed in to change notification settings - Fork 27
/
fdsc.user.js
327 lines (307 loc) · 16.1 KB
/
fdsc.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// ==UserScript==
// @name Flag Dialog Smokey Controls
// @desc Adds Smokey status of a post and feedback options to flag dialogs.
// @author ArtOfCode
// @version 0.14.1
// @updateURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/fdsc.user.js
// @downloadURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/fdsc.user.js
// @supportURL https://github.com/Charcoal-SE/Userscripts/issues
// @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/*
// @exclude *://blog.stackoverflow.com/*
// @require https://cdn.rawgit.com/ofirdagan/cross-domain-local-storage/d779a81a6383475a1bf88595a98b10a8bd5bb4ae/dist/scripts/xdLocalStorage.min.js
// ==/UserScript==
(function() {
'use strict';
var userscript = function($) {
window.fdsc = {};
fdsc.metasmokeKey = "070f26ebb71c5e6cfca7893fe1139460cf23f30d686566f5707a4acfd50c";
/*!
* Given a DOM element containing the post in question, will construct the URL to that post in the form
* required by metasmoke. For questions and answers, respectively:
*
* //stackoverflow.com/questions/12345
* //stackoverflow.com/a/12345
*
*/
fdsc.constructUrl = function(postContainer) {
var base = "//" + location.host + "/";
if ($(postContainer).hasClass("answer")) {
return base + "a/" + $(postContainer).data("answerid");
}
else if ($(postContainer).hasClass("question")) {
return base + "questions/" + $(postContainer).data("questionid");
}
else {
return "";
}
};
/*!
* Given a blurb and a callback method, will prompt the user for input using an SE native prompt and the
* text of the blurb. The callback will be invoked once the input is submitted, and the first parameter
* will contain the submitted data.
*/
fdsc.input = function(blurb, callback) {
function loaded() {
$("#fdsc-popup-submit").on("click", function() {
StackExchange.helpers.closePopups('#fdsc-popup-prompt');
callback($("#fdsc-popup-input").val());
$("#fdsc-popup-submit").off("click");
});
}
$("body").loadPopup({
'lightbox': false,
'target': $("body"),
'html': '<div class="popup fdsc-popup" id="#fdsc-popup-prompt"><p>' + blurb + '</p><input type="text" id="fdsc-popup-input" /><br/><button id="fdsc-popup-submit">OK</button></div>',
'loaded': loaded
});
};
fdsc.confirm = function(blurb, callback) {
function loaded() {
$("#fdsc-popup-ok").on("click", function() {
StackExchange.helpers.closePopups('#fdsc-popup-confirm');
callback(true);
$("#fdsc-popup-ok").off("click");
});
$("#fdsc-popup-cnl").on("click", function() {
StackExchange.helpers.closePopups('#fdsc-popup-confirm');
callback(false);
$("#fdsc-popup-cnl").off("click");
});
}
$("body").loadPopup({
'lightbox': false,
'target': $("body"),
'html': '<div class="popup fdsc-popup" id="fdsc-popup-confirm"><p>' + blurb + '</p><button style="margin:5px;" id="fdsc-popup-ok">OK</button><button style="margin:5px;" id="fdsc-popup-cnl">Cancel</button></div>',
'loaded': loaded
});
};
/*!
* The token that allows us to perform write operations using the metasmoke API. Obtained via MicrOAuth.
* `localStorage` call is left in for backwards compatibility. It's overwritten later.
*/
fdsc.msWriteToken = localStorage.getItem("fdsc_msWriteToken");
/*!
* Obtains a write token and stores it both in `fdsc.msWriteToken` and `xdLocalStorage['fdsc_msWriteToken']`.
* _May_ cause problems with popup blockers, because the window opening isn't triggered by a click... we'll
* have to see how much of a problem that is.
*/
fdsc.getWriteToken = function(afterFlag, callback) {
console.log("getWriteToken");
var w = window.open("https://metasmoke.erwaysoftware.com/oauth/request?key=" + fdsc.metasmokeKey, "_blank");
function getInput() {
fdsc.input("Once you've authenticated FDSC with metasmoke, you'll be given a code; enter it here.", function(code) {
console.log("input callback: " + code);
$.ajax({
'url': 'https://metasmoke.erwaysoftware.com/oauth/token?key=' + fdsc.metasmokeKey + '&code=' + code,
'method': 'GET'
})
.done(function(data) {
fdsc.msWriteToken = data['token'];
xdLocalStorage.setItem("fdsc_msWriteToken", data['token'], function() {
callback();
});
})
.error(function(jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 404) {
StackExchange.helpers.showErrorMessage($(".topbar"), "metasmoke could not find a write token - did you authorize the app?", {
'position': 'toast',
'transient': true,
'transientTimeout': 10000
});
}
else {
StackExchange.helpers.showErrorMessage($(".topbar"), "An unknown error occurred during OAuth with metasmoke.", {
'position': 'toast',
'transient': true,
'transientTimeout': 10000
});
console.log(jqXHR.status, jqXHR.responseText);
}
});
});
}
if (afterFlag) {
$(document).on("DOMNodeRemoved", function(ev) {
if ($(ev.target).attr("id") == "popup-flag-post") {
getInput();
$(document).off("DOMNodeRemoved");
}
});
}
else {
getInput();
}
};
/*!
* Given a Smokey-recognized feedback type, sends that feedback to metasmoke via the API. Requires a valid
* API key and write token; if you don't have these before this is called, get hold of them. A write token
* can be obtained using `fdsc.getWriteToken()`.
*/
fdsc.sendFeedback = function(feedbackType, postId) {
console.log("sendFeedback");
console.log("fdsc.msWriteToken: ", fdsc.msWriteToken);
var token;
if (typeof(fdsc.msWriteToken) == "object") {
token = fdsc.msWriteToken['value'];
}
else {
token = fdsc.msWriteToken;
}
$.ajax({
'type': 'POST',
'url': 'https://metasmoke.erwaysoftware.com/api/w/post/' + postId + '/feedback',
'data': {
'type': feedbackType,
'key': fdsc.metasmokeKey,
'token': token
}
})
.done(function(data) {
StackExchange.helpers.showSuccessMessage($(".topbar"), "Fed back " + feedbackType + " to metasmoke.", {
'position': 'toast',
'transient': true,
'transientTimeout': 10000
});
console.log(data);
})
.error(function(jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 401) {
StackExchange.helpers.showErrorMessage($(".topbar"), "Can't send feedback to metasmoke - not authenticated.", {
'position': 'toast',
'transient': true,
'transientTimeout': 10000
});
console.error("fdsc.sendFeedback was called without having a valid write token");
fdsc.confirm("Write token invalid. Attempt re-authentication?", function(result) {
if (result) {
fdsc.getWriteToken(false, function() {
fdsc.sendFeedback(feedbackType, postId);
});
}
});
}
else {
StackExchange.helpers.showErrorMessage($(".topbar"), "An error occurred sending post feedback to metasmoke.", {
'position': 'toast',
'transient': true,
'transientTimeout': 10000
});
console.log(jqXHR.status, jqXHR.responseText);
}
});
};
/*!
* Well this is a mess.
*/
xdLocalStorage.init({
'iframeUrl': 'https://metasmoke.erwaysoftware.com/xdom_storage.html',
'initCallback': function() {
xdLocalStorage.getItem("fdsc_msWriteToken", function(data) {
fdsc.msWriteToken = data['value'];
console.log("fdsc.msWriteToken: ", data['value']);
});
$(".flag-post-link").on("click", function(clickEvent) {
$(document).on("DOMNodeInserted", function(nodeEvent) {
var postId;
if ($(nodeEvent.target).hasClass("popup") && $(nodeEvent.target).attr("id") == "popup-flag-post") {
var container = $(clickEvent.target).parents(".question, .answer").first();
$.ajax({
'type': 'GET',
'url': 'https://metasmoke.erwaysoftware.com/api/posts/url',
'data': {
'url': fdsc.constructUrl(container),
'key': fdsc.metasmokeKey
}
})
.done(function(data) {
if (data.length > 0 && data[0].id) {
postId = data[0].id;
$.ajax({
'type': 'GET',
'url': 'https://metasmoke.erwaysoftware.com/api/post/' + postId + '/feedback',
'data': {
'key': fdsc.metasmokeKey
}
})
.done(function(data) {
// We use the first char of feedback to identify its type because that's what metasmoke does.
var tps = data.filter(function(el) { return el.feedback_type.indexOf('t') === 0; }).length;
var fps = data.filter(function(el) { return el.feedback_type.indexOf('f') === 0; }).length;
var naa = data.filter(function(el) { return el.feedback_type.indexOf('n') === 0; }).length;
$(".popup-actions").prepend("<div style='float:left' id='smokey-report'><strong>Smokey report: <span style='color:darkgreen'>" + tps + " tp</span>, <span style='color:red'>" + fps + " fp</span>, <span style='color:#7c5500'>" + naa + " naa</span></strong></div>");
})
.error(function(jqXHR, textStatus, errorThrown) {
StackExchange.helpers.showErrorMessage($(".topbar"), "An error occurred fetching post feedback from metasmoke.", {
'position': 'toast',
'transient': true,
'transientTimeout': 10000
});
console.log(jqXHR.status, jqXHR.responseText);
});
}
})
.error(function(jqXHR, textStatus, errorThrown) {
StackExchange.helpers.showMessage($(".topbar"), "An error occurred fetching post ID from metasmoke - has the post been reported by Smokey?", {
'position': 'toast',
'transient': true,
'transientTimeout': 10000,
'type': 'warning'
});
console.error(jqXHR.status, jqXHR.responseText);
});
// We should remove the DOMNodeInserted handler when we're done with it to avoid multiple fires of
// the same handler caused by re-adding it each time you click the flag link.
$(document).off("DOMNodeInserted");
}
$(".popup-submit").on("click", function(ev) {
var selected = $("input[name=top-form]").filter(":checked");
var feedbackType;
if (selected.val() == "PostSpam" || selected.val() == "PostOffensive") {
feedbackType = "tpu-";
}
else if (selected.val() == "AnswerNotAnAnswer") {
feedbackType = "naa-";
}
if (feedbackType && $('#smokey-report').length > 0) {
// because it looks like xdls returns null as a string for some reason
if (!fdsc.msWriteToken || fdsc.msWriteToken == 'null') {
fdsc.getWriteToken(true, function() {
fdsc.sendFeedback(feedbackType, postId);
});
}
else {
fdsc.sendFeedback(feedbackType, postId);
}
}
// Likewise, remove this handler when it's finished to avoid multiple fires.
$(".popup-submit").off("click");
});
});
});
}
});
};
/*!
* This is here because since we're injecting the userscript into the page, we also need to inject
* any libraries we need.
*/
var sourceEl = document.createElement("script");
sourceEl.type = "application/javascript";
sourceEl.src = "https://cdn.rawgit.com/ofirdagan/cross-domain-local-storage/d779a81a6383475a1bf88595a98b10a8bd5bb4ae/dist/scripts/xdLocalStorage.min.js";
sourceEl.onload = function() {
var el = document.createElement("script");
el.type = "application/javascript";
el.text = "(" + userscript + ")(jQuery);";
document.body.appendChild(el);
};
document.body.appendChild(sourceEl);
})();