9gag - Return dislike count

Display the like and dislike counts next to the arrow buttons

目前为 2023-05-18 提交的版本。查看 最新版本

// ==UserScript==
// @name         9gag - Return dislike count
// @namespace    https://github.com/Procyon-b
// @version      0.1
// @description  Display the like and dislike counts next to the arrow buttons
// @author       Achernar
// @match        https://9gag.com/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
"use strict";

// JSON.parse
var JP=JSON.parse;
JSON.parse=function(){
  var r=JP(...arguments);

  if (r && r.data && r.data.posts) {
    for (let v,k=0; v=r.data.posts[k]; k++) {
      posts[v.id]={id: v.id, up: v.upVoteCount, down: v.downVoteCount};
      }
    }

  return r;
}




var posts={};
window._posts=posts;

function init() {
  var obs = new MutationObserver(function(muts){
    for (let mut of muts) {
      if (mut.addedNodes.length) {
        getBtnV(mut.target);
        break;
        }
      }
    });
  obs.observe(document.body, {subtree: true, childList: true} );

  addSt();
  getBtnV();
  }

if (document.readyState != 'loading') init('"already done"', 1);
else {
  document.addEventListener('DOMContentLoaded', (ev) => { init('DOM');} );
  window.addEventListener('load', (ev) => { init('wLoad', 1);} );
  }

function getBtnV(r=document) {
  var a=r.querySelectorAll(':first-child.btn-vote:not(._fixed)');
  for (let n of a) {
    n.classList.add('_fixed');
    let p=n.closest('article');
    if (p) {
      let id= (p.id || '').split('-').pop();
      if (posts[id]) {
        n.classList.add('_exists');
        n.style='--upvotes: "'+posts[id].up+'"; --downvotes: "'+posts[id].down+'";';
        }
      }
    }
  }

var ST, iST=`
._fixed._exists li::before {
  content: var(--upvotes);
  }
._fixed._exists li::after {
  content: var(--downvotes);
  }
`;

function addSt() {
console.info('addSt()');
  if (!iST) {
    document.documentElement.appendChild(ST);
    return;
    }
  try {
    ST=document.createElement('style');
    document.documentElement.appendChild(ST);
    ST.textContent=iST;
    iST='';
  }catch(e){
    setTimeout(addSt,0); }
}


})();