Skip to content

Commit

Permalink
Merge pull request #84 from Charcoal-SE/use-debug
Browse files Browse the repository at this point in the history
Use debug from npm instead of custom debug solutions
  • Loading branch information
j-f1 committed May 24, 2017
2 parents 6bd6198 + 8572d9f commit e32e59b
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 94 deletions.
3 changes: 2 additions & 1 deletion autoflagging/autoflagging.meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// @contributor angussidney
// @contributor ArtOfCode
// @contributor Cerbrus
// @version 0.14.9
// @version 0.14.10
// @updateURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/autoflagging/autoflagging.meta.js
// @downloadURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/autoflagging/autoflagging.user.js
// @supportURL https://github.com/Charcoal-SE/Userscripts/issues
Expand All @@ -17,5 +17,6 @@
// @match *://chat.stackexchange.com/rooms/56223/the-spam-blot*
// @match *://chat.stackoverflow.com/rooms/111347/sobotics*
// @require https://cdn.rawgit.com/joewalnes/reconnecting-websocket/f8055b77ba75e5d564ffb50d20a483bdd7edccdf/reconnecting-websocket.min.js
// @require https://wzrd.in/standalone/debug@%5E2.6.6
// @grant none
// ==/UserScript==
68 changes: 25 additions & 43 deletions autoflagging/autoflagging.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// @contributor angussidney
// @contributor ArtOfCode
// @contributor Cerbrus
// @version 0.14.9
// @version 0.14.10
// @updateURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/autoflagging/autoflagging.meta.js
// @downloadURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/autoflagging/autoflagging.user.js
// @supportURL https://github.com/Charcoal-SE/Userscripts/issues
Expand All @@ -17,17 +17,23 @@
// @match *://chat.stackexchange.com/rooms/56223/the-spam-blot*
// @match *://chat.stackoverflow.com/rooms/111347/sobotics*
// @require https://cdn.rawgit.com/joewalnes/reconnecting-websocket/f8055b77ba75e5d564ffb50d20a483bdd7edccdf/reconnecting-websocket.min.js
// @require https://wzrd.in/standalone/debug@%5E2.6.6
// @grant none
// ==/UserScript==

/* global autoflagging, ReconnectingWebSocket */
/* global autoflagging, ReconnectingWebSocket, unsafeWindow */

// To enable/disable trace information, type autoflagging.trace(true) or
// autoflagging.trace(false), respectively, in your browser's console.

(function () {
"use strict";
// console.log("Autoflagging Information started.");
const createDebug = typeof unsafeWindow === "undefined" ? window.debug : unsafeWindow.debug || window.debug;
const debug = createDebug("aim");
debug.decorate = createDebug("aim:decorate");
debug.ws = createDebug("aim:ws");
debug.queue = createDebug("aim:queue");
debug("started");

// Inject CSS
var css = window.document.createElement("link");
Expand All @@ -45,26 +51,6 @@
// Constants
var hOP = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty);
window.autoflagging = {};
autoflagging.traceKey = "autoflagging-trace";
autoflagging.trace = function (enable) {
// Enable/disable trace
if (enable) {
console.log("Autoflagging trace enabled.");
window.localStorage.setItem(autoflagging.traceKey, "true");
} else {
console.log("Autoflagging trace disabled.");
window.localStorage.removeItem(autoflagging.traceKey);
}
return enable;
};
autoflagging.log = function (object) {
// Trace enabled?
if (window.localStorage.getItem(autoflagging.traceKey) !== "true") {
return;
}
console.log("Time: " + new Date().toISOString().substring(11, 23));
console.log(object);
};
autoflagging.smokeyIds = { // this is Smokey's user ID for each supported domain
"chat.stackexchange.com": 120914,
"chat.stackoverflow.com": 3735529,
Expand All @@ -89,8 +75,7 @@
* `element` is a message (i.e. has the .message class)
*/
autoflagging.decorateMessage = function ($message, data) {
autoflagging.log("DECORATE " + JSON.stringify(data));
autoflagging.log($message);
debug.decorate(data, $message);

autoflagging.decorate($message.children(".ai-information"), data);
autoflagging.decorate($message.find(".meta .ai-information"), data);
Expand Down Expand Up @@ -130,7 +115,7 @@
}
})
.fail(function (xhr) {
autoflagging.log("Failed to load reasons: " + xhr.statusText);
debug("Failed to load reasons:", xhr);
});
}
};
Expand Down Expand Up @@ -339,8 +324,7 @@
* message itself and to the 'meta'-element shown on hovering over the message.
*/
autoflagging.addSpinnerToMessage = function ($message) {
autoflagging.log("ADD SPINNER");
autoflagging.log($message);
debug("add spinner to", $message);
autoflagging.addSpinner($message);
autoflagging.addSpinner($message.find(".meta"), true);
};
Expand All @@ -366,13 +350,13 @@
* It will use the results to decorate the Smokey reports which are already on the page.
*/
autoflagging.callAPI = function (urls, page) {
autoflagging.log("Call API");
debug("Call API");
if (page == null) {
page = 1;
}
var autoflagData = {};
var url = autoflagging.baseURL + "&page=" + page + "&urls=" + urls;
autoflagging.log("URL: " + url);
debug("URL:", url);
$.get(url, function (data) {
// Group information by link
for (var i = 0; i < data.items.length; i++) {
Expand All @@ -397,7 +381,7 @@
autoflagging.callAPI(urls, ++page);
}
}).fail(function (xhr) {
autoflagging.notify("Failed to load data: " + xhr.statusText);
autoflagging.notify("Failed to load data:", xhr);
});
};

Expand Down Expand Up @@ -468,13 +452,12 @@
autoflagging.socket.onmessage = function (message) {
function decorate(selector, data) {
(function _deco() {
autoflagging.log("Attempting to decorate \"" + selector + "\" with " + JSON.stringify(data));
autoflagging.log($(selector).parents(".message"));
debug.decorate("Attempting to decorate \"" + selector + "\" with", data, "message:", $(selector).parents(".message"));
if ($(selector).parents(".message").find(".ai-spinner, .ai-information.ai-loaded").length > 0) {
autoflagging.decorateMessage($(selector).parents(".message"), data);
} else {
// MS is faster than chat; add the decorate operation to the queue
autoflagging.log("Queueing " + selector);
debug.queue("Queueing", selector);
autoflagging.msgQueue.push(_deco);
}
})();
Expand All @@ -489,31 +472,31 @@
break;
default: {
// Analyze socket message
autoflagging.log(jsonData.message);
debug.ws("got message", jsonData.message);
var flagLog = jsonData.message.flag_log;
var deletionLog = jsonData.message.deletion_log;
var feedback = jsonData.message.feedback;
var notFlagged = jsonData.message.not_flagged;
if (typeof flagLog != "undefined") {
// Autoflagging information
autoflagging.log(flagLog.user_name + " autoflagged " + flagLog.post.link);
debug.ws(flagLog.user, "autoflagged", flagLog.post);
let selector = autoflagging.selector + "a[href^='" + flagLog.post.link + "']";
decorate(selector, flagLog.post);
} else if (typeof deletionLog != "undefined") {
// Deletion log
autoflagging.log(deletionLog.post_link + " deleted");
debug.ws("deleted:", deletionLog);
let selector = autoflagging.selector + "a[href^='" + deletionLog.post_link + "']";
$(selector).parents(".content").addClass("ai-deleted");
} else if (typeof feedback != "undefined") {
// Feedback
autoflagging.log(feedback.user_name + " posted " + feedback.symbol + " on " + feedback.post_link, feedback); // feedback_type
debug.ws(feedback.user, "posted", feedback.symbol, "on", feedback.post_link, feedback); // feedback_type
let selector = autoflagging.selector + "a[href^='" + feedback.post_link + "']";
decorate(selector, {
feedbacks: [feedback]
});
} else if (typeof notFlagged != "undefined") {
// Not flagged
autoflagging.log(notFlagged.post.link + " not flagged");
debug.ws(notFlagged.post, "not flagged");
let selector = autoflagging.selector + "a[href^='" + notFlagged.post.link + "']";
decorate(selector, notFlagged.post);
}
Expand All @@ -523,7 +506,7 @@
};

autoflagging.socket.onopen = function () {
autoflagging.log("WebSocket opened.");
debug.ws("WebSocket opened.");
// Send authentication
autoflagging.socket.send(JSON.stringify({
identifier: JSON.stringify({
Expand All @@ -534,7 +517,7 @@
}));
};
autoflagging.socket.onclose = function (close) {
autoflagging.log("WebSocket closed: " + close.code + " - " + close.reason + ".");
debug.ws("WebSocket closed:", close);
};

// Sometimes, autoflagging information arrives before the chat message.
Expand All @@ -554,8 +537,7 @@
var args = arguments;
q.forEach(function (f) {
setTimeout(function () {
autoflagging.log("Resolving queue: " + JSON.stringify(args));
autoflagging.log(f);
debug.queue("Resolving queue:", f, args);
f.apply(self, args);
}, 100);
});
Expand Down
4 changes: 3 additions & 1 deletion fdsc/fdsc.meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
// @author ArtOfCode
// @contributor angussidney
// @contributor rene
// @contributor J F
// @attribution Brock Adams (https://github.com/BrockA)
// @version 1.14.0
// @version 1.14.1
// @updateURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/fdsc/fdsc.meta.js
// @downloadURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/fdsc/fdsc.user.js
// @supportURL https://github.com/Charcoal-SE/Userscripts/issues
Expand All @@ -23,5 +24,6 @@
// @exclude *://blog.stackoverflow.com/*
// @exclude *://*.area51.stackexchange.com/*
// @require https://cdn.rawgit.com/ofirdagan/cross-domain-local-storage/d779a81a6383475a1bf88595a98b10a8bd5bb4ae/dist/scripts/xdLocalStorage.min.js
// @require https://wzrd.in/standalone/debug@%5E2.6.6
// @grant none
// ==/UserScript==
36 changes: 20 additions & 16 deletions fdsc/fdsc.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
// @author ArtOfCode
// @contributor angussidney
// @contributor rene
// @contributor J F
// @attribution Brock Adams (https://github.com/BrockA)
// @version 1.14.0
// @version 1.14.1
// @updateURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/fdsc/fdsc.meta.js
// @downloadURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/fdsc/fdsc.user.js
// @supportURL https://github.com/Charcoal-SE/Userscripts/issues
Expand All @@ -23,14 +24,17 @@
// @exclude *://blog.stackoverflow.com/*
// @exclude *://*.area51.stackexchange.com/*
// @require https://cdn.rawgit.com/ofirdagan/cross-domain-local-storage/d779a81a6383475a1bf88595a98b10a8bd5bb4ae/dist/scripts/xdLocalStorage.min.js
// @require https://wzrd.in/standalone/debug@%5E2.6.6
// @grant none
// ==/UserScript==

/* global fdsc, $, xdLocalStorage, confirm */
/* global fdsc, $, xdLocalStorage, confirm, unsafeWindow */
/* eslint-disable max-nested-callbacks */

(function () {
"use strict";
const createDebug = typeof unsafeWindow === "undefined" ? window.debug : unsafeWindow.debug || window.debug;
const debug = createDebug("fdsc");

var userscript = function ($) {
window.fdsc = {};
Expand Down Expand Up @@ -111,12 +115,12 @@
* have to see how much of a problem that is.
*/
fdsc.getWriteToken = function (afterFlag, callback) {
console.log("getWriteToken");
debug("entering getWriteToken");
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);
debug("input callback: " + code);
$.ajax({
url: "https://metasmoke.erwaysoftware.com/oauth/token?key=" + fdsc.metasmokeKey + "&code=" + code,
method: "GET"
Expand All @@ -138,7 +142,7 @@
transient: true,
transientTimeout: 10000
});
console.log(jqXHR.status, jqXHR.responseText);
debug("getting write token failed", jqXHR);
}
});
});
Expand All @@ -162,8 +166,8 @@
* can be obtained using `fdsc.getWriteToken()`.
*/
fdsc.sendFeedback = function (feedbackType, postId) {
console.log("sendFeedback");
console.log("fdsc.msWriteToken: ", fdsc.msWriteToken);
debug("entering sendFeedback");
debug("write token:", fdsc.msWriteToken);
var token;
if (typeof (fdsc.msWriteToken) === "object") {
token = fdsc.msWriteToken.value;
Expand All @@ -185,7 +189,7 @@
transient: true,
transientTimeout: 10000
});
console.log(data);
debug("feedback sent:", data);
$(window.event.target).attr("data-fdsc-ms-id", null);
fdsc.postFound = null;
}).error(function (jqXHR) {
Expand All @@ -209,7 +213,7 @@
transient: true,
transientTimeout: 10000
});
console.log(jqXHR.status, jqXHR.responseText);
debug("feedback failed:", jqXHR);
}
$(window.event.target).attr("data-fdsc-ms-id", null);
fdsc.postFound = null;
Expand All @@ -225,8 +229,8 @@
if (StackExchange.options.user.isModerator) {
return;
}
console.log("reportPost");
console.log("fdsc.msWriteToken: ", fdsc.msWriteToken);
debug("entering reportPost");
debug("write token:", fdsc.msWriteToken);
var token;
if (typeof (fdsc.msWriteToken) === "object") {
token = fdsc.msWriteToken.value;
Expand All @@ -248,7 +252,7 @@
transient: true,
transientTimeout: 10000
});
console.log(data);
debug("post reported", data);
}).error(function (jqXHR) {
if (jqXHR.status === 401) {
StackExchange.helpers.showErrorMessage($(".topbar"), "Can't report post to metasmoke - not authenticated.", {
Expand All @@ -270,7 +274,7 @@
transient: true,
transientTimeout: 10000
});
console.log(jqXHR.status, jqXHR.responseText);
debug("report failed:", jqXHR);
}
});
};
Expand All @@ -283,7 +287,7 @@
initCallback: function () {
xdLocalStorage.getItem("fdsc_msWriteToken", function (data) {
fdsc.msWriteToken = data.value;
console.log("fdsc.msWriteToken: ", data.value);
debug("write token", data.value);
});

$(".flag-post-link").on("click", function (clickEvent) {
Expand All @@ -303,7 +307,7 @@

function registerFeedbackButton(buttonSelector, feedback, logMessage) {
$(buttonSelector).on("click", function (ev) {
console.log(logMessage);
debug("feedback clicked:", logMessage);
ev.preventDefault();
if (!fdsc.msWriteToken || fdsc.msWriteToken === "null") {
fdsc.getWriteToken(true, function () {
Expand Down Expand Up @@ -362,7 +366,7 @@
$("#get-write-token").on("click", function (ev) {
ev.preventDefault();
fdsc.getWriteToken(false, function () {
console.log(clickEvent);
debug("click event", clickEvent);

$(".popup-close a").click();
$(clickEvent.currentTarget).click();
Expand Down
3 changes: 2 additions & 1 deletion sds/sds.meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// @namespace https://github.com/Charcoal-SE/
// @description Show the status of all SmokeDetector instances
// @author J F
// @version 0.0.1
// @version 0.0.3
// @updateURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/sds/sds.meta.js
// @downloadURL https://raw.githubusercontent.com/Charcoal-SE/Userscripts/master/sds/sds.user.js
// @supportURL https://github.com/Charcoal-SE/Userscripts/issues
Expand All @@ -12,5 +12,6 @@
// @match *://chat.meta.stackexchange.com/rooms/89/tavern-on-the-meta*
// @match *://chat.stackexchange.com/rooms/56223/the-spam-blot*
// @require https://wzrd.in/standalone/actioncable@5.1.0
// @require https://wzrd.in/standalone/debug@%5E2.6.6
// @grant none
// ==/UserScript==

0 comments on commit e32e59b

Please sign in to comment.