From a29cee6d60d091061be29ef4dc85d53ab0b6841b Mon Sep 17 00:00:00 2001 From: Makyen Date: Thu, 31 Oct 2019 21:28:00 -0700 Subject: [PATCH 1/7] SIM: Display 'No Feedback' when there's no feedback --- sim/sim.user.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sim/sim.user.js b/sim/sim.user.js index a402d6e..1d3cf9b 100644 --- a/sim/sim.user.js +++ b/sim/sim.user.js @@ -136,7 +136,10 @@ const feedbacks = feedbacksJson.items; const uniques = [...(new Set(feedbacks.map(f => f.feedback_type.charAt(0))))]; let fbType; - if (uniques.length === 1) { + if (uniques.length === 0) { + fbType = 'No Feedback'; + } + else if (uniques.length === 1) { fbType = feedbacks[0].feedback_type; } else { From 1720f3bc4101e85409ae4e9b8dc907fecfc30d3c Mon Sep 17 00:00:00 2001 From: Makyen Date: Thu, 31 Oct 2019 21:30:36 -0700 Subject: [PATCH 2/7] SIM: Add $ to globals --- sim/sim.user.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sim/sim.user.js b/sim/sim.user.js index 1d3cf9b..31346af 100644 --- a/sim/sim.user.js +++ b/sim/sim.user.js @@ -21,7 +21,7 @@ // @downloadURL https://github.com/Charcoal-SE/userscripts/raw/master/sim/sim.user.js // ==/UserScript== -/* globals StackExchange */ +/* globals StackExchange, $ */ (() => { const msAPIKey = '5a70b21ec1dd577d6ce36d129df3b0262b7cec2cd82478bbd8abdc532d709216'; From c1b8c11a7e5441076dda6f81c72ac7ad0531527a Mon Sep 17 00:00:00 2001 From: Makyen Date: Thu, 31 Oct 2019 21:34:32 -0700 Subject: [PATCH 3/7] SIM: re-indent stacksModal --- sim/sim.user.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/sim/sim.user.js b/sim/sim.user.js index 31346af..2b82158 100644 --- a/sim/sim.user.js +++ b/sim/sim.user.js @@ -58,13 +58,14 @@ }); }; - const stacksModal = $(``); + const stacksModal = $(` + `); const displayDialog = postData => { const modal = stacksModal.clone(); From d3a4e09f1fbce0428cca4b5031de26a5ec1d318d Mon Sep 17 00:00:00 2001 From: Makyen Date: Thu, 31 Oct 2019 21:43:58 -0700 Subject: [PATCH 4/7] SIM: improve attaching to posts Attach to posts: earlier in page load after inline edits after other page modification on NATO w/ and w/o NATO Enhancements handle the .post-menu-container which SE may, or may not, use in review queues in close-vote popups --- sim/sim.user.js | 98 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 6 deletions(-) diff --git a/sim/sim.user.js b/sim/sim.user.js index 2b82158..cffed8d 100644 --- a/sim/sim.user.js +++ b/sim/sim.user.js @@ -26,6 +26,8 @@ (() => { const msAPIKey = '5a70b21ec1dd577d6ce36d129df3b0262b7cec2cd82478bbd8abdc532d709216'; + const isNato = location.pathname === '/tools/new-answers-old-questions'; + const getCurrentSiteAPIParam = () => { const regex = /((?:meta\.)?(?:(?:(?:math|stack)overflow|askubuntu|superuser|serverfault)|\w+)(?:\.meta)?)\.(?:stackexchange\.com|com|net)/g; const exceptions = { @@ -48,13 +50,83 @@ return null; }; + const getPostMenu = $e => { + return $e.find('.post-menu:not(.preview-options)').map(function () { + // SE has used a .post-menu-container within the .post-menu. It was there for a while and then removed. + // It's not clear if it will come back. This is just playing it safe in case SE puts it back in. + const container = $(this).children('.post-menu-container'); + if (container.length > 0) { + return container; + } + return this; + }); + }; + + const getPostId = e => { + const $e = $(e); + let post = $e; + if (!post.is('.question, .answer')) { + post = $e.closest('.answer, .question'); + } + let id = post.data('questionid') || post.data('answerid'); + if (!id) { + // If we are passed a .post-menu, then get the child that is the js-share-link, as it's definitely associated with the post. + let link = $e.children('.js-share-link'); + if (link.length === 0) { + link = post.find('.answer-hyperlink, .question-hyperlink'); + } + if (link.length === 0) { + link = $e.find('.js-share-link'); + } + if (link.length === 0) { + return null; + } + const href = link.attr('href'); + const endNumberMatch = href.match(/#(\d+)$/); + if (endNumberMatch) { + id = endNumberMatch[1]; + } else { + const firstNumberMatch = href.match(/(\d+)/); + id = firstNumberMatch && firstNumberMatch[1]; + } + } + return id; + }; + const attachToPosts = () => { - $('.question, .answer').each((i, e) => { - const id = $(e).data(`${$(e).hasClass('question') ? 'question' : 'answer'}id`); - const apiParam = getCurrentSiteAPIParam(); - const msUri = `https://metasmoke.erwaysoftware.com/api/v2.0/posts/uid/${apiParam}/${id}?key=${msAPIKey}`; + let posts = $('.question, .answer'); + if (posts.length === 0 && isNato) { + $('body.tools-page #mainbar > table.default-view-post-table > tbody > tr').addClass('answer'); + posts = $('.question, .answer'); + } + posts.each((i, e) => { + const $e = $(e); - $(e).find('.post-menu').append(`|smokey`); + // Get the element which contains the menu + let postMenu = getPostMenu($e); + if (postMenu.length === 0 && isNato) { + $e.find('> td:last-of-type').append($('
')); + postMenu = getPostMenu($e); + } + postMenu.filter(function () { + // Don't re-add the smokey button if it's already there. + return !$(this).find('.sim-get-info').length; + // Add the smokey button to the remaining post menus. + }).each(function () { + // We construct the msUri in here, because there are some cases where there can be a .question within a .question. + const id = getPostId(this); + if (!id) { + return; + } + const apiParam = getCurrentSiteAPIParam(); + const msUri = `https://metasmoke.erwaysoftware.com/api/v2.0/posts/uid/${apiParam}/${id}?key=${msAPIKey}`; + const $this = $(this); + $this.append(`|smokey`); + if (isNato) { + // Clean up if we are in NATO Enhancements + $this.closest('body.tools-page #mainbar > table.default-view-post-table > tbody > tr.answer .question').closest('tr.answer').removeClass('answer'); + } + }); }); }; @@ -166,8 +238,22 @@ displayDialog(postData); }; + attachToPosts(); $(document).ready(() => { attachToPosts(); - $('.sim-get-info').on('click', getInfo); + $(document) + .on('click', '.sim-get-info', getInfo) + // Most, but not all, cases where the button needs to be re-added happen after an AJAX call. + .ajaxComplete(() => { + attachToPosts(); + // Some AJAX fetches need a delayed call. + setTimeout(attachToPosts, 55); + }); + // There are some corner cases where adding the button needs to be done after SE is ready. + StackExchange.ready(() => { + attachToPosts(); + // Some pages (e.g. NATO) just take a long time to be ready. + setTimeout(attachToPosts, 5000); + }); }); })(); From 551ef4d8d907e2d6a774501b262420494b3f3db3 Mon Sep 17 00:00:00 2001 From: Makyen Date: Thu, 31 Oct 2019 21:55:47 -0700 Subject: [PATCH 5/7] SIM: Makyen is a contributor --- sim/sim.user.js | 1 + 1 file changed, 1 insertion(+) diff --git a/sim/sim.user.js b/sim/sim.user.js index cffed8d..22b90d1 100644 --- a/sim/sim.user.js +++ b/sim/sim.user.js @@ -4,6 +4,7 @@ // @version 0.4.2 // @description Dig up information about how SmokeDetector handled a post. // @author ArtOfCode +// @contributor Makyen // @match *://*.stackexchange.com/* // @match *://*.stackoverflow.com/* // @match *://*.superuser.com/* From 0ce18814cd45afccec316e4f9d3fb24d2497f28e Mon Sep 17 00:00:00 2001 From: Makyen Date: Thu, 31 Oct 2019 22:49:47 -0700 Subject: [PATCH 6/7] SIM: Don't run on Teams --- sim/sim.user.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sim/sim.user.js b/sim/sim.user.js index 22b90d1..d624ec2 100644 --- a/sim/sim.user.js +++ b/sim/sim.user.js @@ -25,6 +25,10 @@ /* globals StackExchange, $ */ (() => { + if (window.location.pathname.indexOf('/c/') === 0) { + // Don't run on Teams + return; + } const msAPIKey = '5a70b21ec1dd577d6ce36d129df3b0262b7cec2cd82478bbd8abdc532d709216'; const isNato = location.pathname === '/tools/new-answers-old-questions'; From ce596f75056a0535b850c13a9555a1920f4b792f Mon Sep 17 00:00:00 2001 From: Makyen Date: Thu, 31 Oct 2019 22:08:30 -0700 Subject: [PATCH 7/7] SIM: Version 0.5.1 --- sim/sim.user.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sim/sim.user.js b/sim/sim.user.js index d624ec2..fdae270 100644 --- a/sim/sim.user.js +++ b/sim/sim.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name SIM - SmokeDetector Info for Moderators // @namespace https://charcoal-se.org/ -// @version 0.4.2 +// @version 0.5.1 // @description Dig up information about how SmokeDetector handled a post. // @author ArtOfCode // @contributor Makyen