9gag - Return dislike count

Display the like and dislike counts next to the arrow buttons. Forces display of poster's name instead of section. Display account's age next to posts and comments

  1. // ==UserScript==
  2. // @name 9gag - Return dislike count
  3. // @namespace https://github.com/Procyon-b
  4. // @version 0.5
  5. // @description Display the like and dislike counts next to the arrow buttons. Forces display of poster's name instead of section. Display account's age next to posts and comments
  6. // @author Achernar
  7. // @match https://9gag.com/*
  8. // @run-at document-start
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. "use strict";
  14.  
  15. // JSON.parse
  16. var JP=JSON.parse;
  17. JSON.parse=function(){
  18. var r=JP(...arguments);
  19. if (r && r.data && r.data.post && r.data.post.interests) {
  20. r.data.post.interests=[];
  21. addProf(r.data.post.creator);
  22. }
  23. if (r && r.data && r.data.posts) {
  24. for (let v,k=0; v=r.data.posts[k]; k++) {
  25. v.interests=[];
  26. posts[v.id]={id: v.id, up: v.upVoteCount, down: v.downVoteCount};
  27. addProf(v.creator);
  28. }
  29. }
  30. try{
  31. if (r.data.items) {
  32. for (let v,k=0; v=r.data.items[k]; k++) {
  33. if (v.post) v.post.interests=[];
  34. }
  35. }
  36. }
  37. catch(e){}
  38.  
  39. if (r && r.payload && r.payload.comments) {
  40. for (let v,k=0; v=r.payload.comments[k]; k++) {
  41. addProf(v.user);
  42. }
  43. }
  44.  
  45. return r;
  46. }
  47.  
  48.  
  49. var posts={}, done=false,
  50. prof={},
  51. ST, iST=`
  52. ._fixed._exists li::before,
  53. ._fixed._exists .post-vote__btn.upvote::before {
  54. content: var(--upvotes);
  55. }
  56. ._fixed._exists li::after,
  57. ._fixed._exists .post-vote__btn.downvote::after {
  58. content: var(--downvotes);
  59. }
  60. ._fixed._exists .post-vote__btn.upvote,
  61. ._fixed._exists .post-vote__btn.downvote {
  62. display: flex;
  63. }
  64. .post-award-users {
  65. display: none !important;
  66. }
  67.  
  68. .ui-post-creator__creation::after,
  69. .post-view-header__head::after,
  70. .ui-comment-header__container .ui-comment-header__time::after {
  71. content: var(--profage);
  72. margin-left: 1em;
  73. color: var(--palette-text-normal);
  74. pointer-events: none;
  75. }
  76. .ui-comment-header__container a.ui-comment-header__time::after {
  77. position: absolute;
  78. }
  79. `;
  80.  
  81. function addProf(v) {
  82. if (v && !prof[v.username || v.displayName]) {
  83. prof[v.username || v.displayName]={v, id: v.accountId, creation: v.creationTs || v.timestamp || (v.userId && v.userId.substr(2,10)), fullName: v.fullName, username: v.username || v.displayName, approx: v.creationTs?0:1 }
  84. }
  85. }
  86.  
  87. function init() {
  88. if (done) return;
  89. else done=true;
  90. var obs = new MutationObserver(function(muts){
  91. var pt=null;
  92. for (let mut of muts) {
  93. if (mut.addedNodes.length) {
  94. if (mut.target != pt) {
  95. pt=mut.target;
  96. getBtnV(mut.target);
  97. }
  98. }
  99. }
  100. });
  101. obs.observe(document.body, {subtree: true, childList: true} );
  102.  
  103. addSt();
  104. getBtnV();
  105. }
  106.  
  107. if (document.readyState != 'loading') init();
  108. else {
  109. document.addEventListener('DOMContentLoaded', init);
  110. window.addEventListener('load', init);
  111. }
  112.  
  113. function getBtnV(r=document) {
  114. var a=r.querySelectorAll(':first-child.btn-vote:not(._fixed), .post-vote:not(.fixed)');
  115. for (let n of a) {
  116. n.classList.add('_fixed');
  117. let p=n.closest('article');
  118. if (p) {
  119. let id= (p.id || '').split('-').pop();
  120. if (!id) { // mobile
  121. let e=p.querySelector(':scope > header > a[href^="/gag/"]');
  122. if (e) id=e.href.split('/').pop();
  123. }
  124. if (posts[id]) {
  125. n.classList.add('_exists');
  126. n.style='--upvotes: "'+posts[id].up+'"; --downvotes: "'+posts[id].down+'";';
  127. }
  128. }
  129. }
  130. a=r.querySelectorAll('.ui-post-creator__author:not(._fixed), .post-view-header__author:not(._fixed), .ui-comment-header__username:not(._fixed)');
  131. for (let n of a) {
  132. n.classList.add('_fixed');
  133. let id=n.href.split('/').pop();
  134. if (prof[id] && parseInt(prof[id].creation) ) {
  135. let d=parseInt( ( Date.now()/1000 - prof[id].creation ) /60/60/24 )
  136. n.parentNode.style='--profage: "('+(prof[id].approx?'~ ':'')+d+' days old)"';
  137. }
  138. }
  139. }
  140.  
  141. function addSt() {
  142. if (!iST) {
  143. return;
  144. }
  145. try {
  146. ST=document.createElement('style');
  147. document.documentElement.appendChild(ST);
  148. ST.textContent=iST;
  149. iST='';
  150. }catch(e){
  151. setTimeout(addSt,0); }
  152. }
  153.  
  154.  
  155. })();