Last active
May 9, 2018 09:37
-
-
Save Aran-Fey/c6f87944795d579f8807d2ba6741a150 to your computer and use it in GitHub Desktop.
A userscript that displays how long it took for a StackOverflow question to be closed as a duplicate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name StackOverflow hammer stopwatch | |
// @description Displays how long it took for a question to be closed as duplicate | |
// @version 1.2 | |
// @author Paul Pinterits | |
// @include *://*.stackexchange.com/questions/* | |
// @include *://meta.serverfault.com/questions/* | |
// @include *://meta.stackoverflow.com/questions/* | |
// @include *://meta.superuser.com/questions/* | |
// @include *://serverfault.com/questions/* | |
// @include *://stackoverflow.com/questions/* | |
// @include *://superuser.com/questions/* | |
// @exclude *://*/questions/tagged/* | |
// @exclude *://*/questions/ask | |
// @grant none | |
// @updateURL https://gist.github.com/Aran-Fey/c6f87944795d579f8807d2ba6741a150/raw/SO_dupehammer_stopwatch.user.js | |
// @downloadURL https://gist.github.com/Aran-Fey/c6f87944795d579f8807d2ba6741a150/raw/SO_dupehammer_stopwatch.user.js | |
// ==/UserScript== | |
function extract_time(node){ | |
var time_node = node.querySelector('.relativetime'); | |
var time = time_node.title; | |
return Date.parse(time); | |
} | |
function format_time(time){ | |
var sec_num = Math.floor(time/1000); | |
var hours = Math.floor(sec_num / 3600); | |
var minutes = Math.floor((sec_num - (hours * 3600)) / 60); | |
var seconds = sec_num - (hours * 3600) - (minutes * 60); | |
var result = ''; | |
if (hours > 0) result += hours + 'h '; | |
if (minutes > 0) result += minutes + ' min '; | |
if (seconds > 0) result += seconds + ' sec '; | |
return result.substr(0, result.length-1); | |
} | |
var question = document.getElementById('question'); | |
var ask_time = extract_time(question.querySelector('.owner')); | |
var seen = []; | |
function show_time(){ | |
for (var status_box of question.querySelectorAll('div.question-status')){ | |
if (status_box === null) | |
continue; | |
if (!status_box.textContent.includes('marked as duplicate')) | |
continue; | |
if (seen.includes(status_box)) | |
continue; | |
seen.push(status_box); | |
var close_time = extract_time(status_box); | |
var duration = close_time - ask_time; | |
var elem = document.createElement('span'); | |
elem.classList.add('relativetime'); | |
elem.textContent = '(Time: '+format_time(duration)+')'; | |
var parent = status_box.querySelector('h2'); | |
parent.appendChild(elem); | |
} | |
} | |
show_time(); | |
function on_dom_mutation(mutations, observer){ | |
for (var mutation of mutations){ | |
if (mutation.addedNodes.length > 0){ | |
show_time(); | |
return; | |
} | |
} | |
} | |
var observer_config = {childList: true, | |
subtree: true}; | |
new MutationObserver(on_dom_mutation).observe(question, observer_config); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment