Better Stack Overflow

Replace time format and add share button to each answer

Per 11-04-2024. Zie de nieuwste versie.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @name         Better Stack Overflow
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Replace time format and add share button to each answer
// @author       Landon Li
// @match        *://stackoverflow.com/questions/*
// @match        *://*.stackexchange.com/questions/*
// @match        *://superuser.com/questions/*
// @match        *://askubuntu.com/questions/*
// @icon         https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    function htmlToElement(html) {
        var template = document.createElement('template');
        html = html.trim(); // Never return a text node of whitespace as the result
        template.innerHTML = html;
        return template.content.firstChild;
    }

    console.log('Replacing time format...');
    var type1TimeSpans = document.evaluate('//div[contains(@class,"user-action-time")]//span', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    for (var i = 0; i < type1TimeSpans.snapshotLength; i++) {
        var type1TimeSpan = type1TimeSpans.snapshotItem(i);
        type1TimeSpan.innerText = type1TimeSpan.title;
    }

    var type2TimeSpans = document.evaluate('//span[@class="comment-date"]//span', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    for (var j = 0; j < type2TimeSpans.snapshotLength; j++) {
        var type2TimeSpan = type2TimeSpans.snapshotItem(j);
        type2TimeSpan.innerText = type2TimeSpan.title.split(',')[0];
    }

    console.log('Adding share buttons...');
    var answerDivs = document.evaluate('//div[@id="answers"]/div[contains(@id, "answer-")]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    for (var j = 0; j < answerDivs.snapshotLength; j++) {
        var answerDiv = answerDivs.snapshotItem(j);
        var answerID = document.evaluate('./@id', answerDiv, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.value;
        var actionDiv = document.evaluate('./div[1]/div[1]/div[1]', answerDiv, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        var shareLink = window.location.href.split('#')[0] + '#' + answerID;
        var shareDiv = htmlToElement('<div class="py6 mx-auto"><a href="#' + answerID + '" onclick="navigator.clipboard.writeText(\'' + shareLink + '\')">🔗</a></div>');
        actionDiv.appendChild(shareDiv);
    }

})();