9gag - Return dislike count

Display the like and dislike counts next to the arrow buttons

当前为 2023-05-24 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         9gag - Return dislike count
// @namespace    https://github.com/Procyon-b
// @version      0.3
// @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={}, done=false,
    ST, iST=`
._fixed._exists li::before,
._fixed._exists .post-vote__btn.upvote::before {
  content: var(--upvotes);
}
._fixed._exists li::after,
._fixed._exists .post-vote__btn.downvote::after {
  content: var(--downvotes);
}
._fixed._exists .post-vote__btn.upvote,
._fixed._exists .post-vote__btn.downvote {
  display: flex;
}
`;

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

  addSt();
  getBtnV();
  }

if (document.readyState != 'loading') init();
else {
  document.addEventListener('DOMContentLoaded', init);
  window.addEventListener('load', init);
  }

function getBtnV(r=document) {
  var a=r.querySelectorAll(':first-child.btn-vote:not(._fixed), .post-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 (!id) { // mobile
        let e=p.querySelector(':scope > header > a[href^="/gag/"]');
        if (e) id=e.href.split('/').pop();
        }
      if (posts[id]) {
        n.classList.add('_exists');
        n.style='--upvotes: "'+posts[id].up+'"; --downvotes: "'+posts[id].down+'";';
        }
      }
    }
  }

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


})();