Better Stack Overflow

Replace time format and add share button to each answer

Verze ze dne 17. 06. 2022. Zobrazit nejnovější verzi.

  1. // ==UserScript==
  2. // @name Better Stack Overflow
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.3
  5. // @description Replace time format and add share button to each answer
  6. // @author Landon Li
  7. // @match *://stackoverflow.com/questions/*
  8. // @match *://webapps.stackexchange.com/questions/*
  9. // @match *://superuser.com/questions/*
  10. // @match *://askubuntu.com/questions/*
  11. // @icon https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. 'use strict';
  17.  
  18. function htmlToElement(html) {
  19. var template = document.createElement('template');
  20. html = html.trim(); // Never return a text node of whitespace as the result
  21. template.innerHTML = html;
  22. return template.content.firstChild;
  23. }
  24.  
  25. console.log('Replacing time format...');
  26. var timeSpans = document.evaluate('//div[@class="user-action-time"]//span', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  27. for (var i = 0; i < timeSpans.snapshotLength; i++) {
  28. var timeSpan = timeSpans.snapshotItem(i);
  29. timeSpan.innerText = timeSpan.title;
  30. }
  31.  
  32. console.log('Adding share buttons...');
  33. var answerDivs = document.evaluate('//div[@id="answers"]/div[contains(@id, "answer-")]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  34. for (var j = 0; j < answerDivs.snapshotLength; j++) {
  35. var answerDiv = answerDivs.snapshotItem(j);
  36. var answerID = document.evaluate('./@id', answerDiv, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.value;
  37. var actionDiv = document.evaluate('./div[1]/div[1]/div[1]', answerDiv, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  38. var shareLink = window.location.href.split('#')[0] + '#' + answerID;
  39. var shareDiv = htmlToElement('<div class="py6 mx-auto"><a href="#' + answerID + '" onclick="navigator.clipboard.writeText(\'' + shareLink + '\')">🔗</a></div>');
  40. actionDiv.appendChild(shareDiv);
  41. }
  42.  
  43. })();