Greasy Fork

Greasy Fork is available in English.

NGA Likes Support

显示被点赞和粉丝数量

当前为 2021-03-05 提交的版本,查看 最新版本

// ==UserScript==
// @name        NGA Likes Support
// @namespace   http://greasyfork.icu/users/263018
// @version     1.0.3
// @author      snyssss
// @description 显示被点赞和粉丝数量

// @match       *://bbs.nga.cn/read.php?tid=*
// @match       *://ngabbs.com/read.php?tid=*
// @match       *://nga.178.com/read.php?tid=*

// @noframes
// ==/UserScript==

((ui) => {
  if (!ui) return;

  const hookFunction = (object, functionName, callback) => {
    ((originalFunction) => {
      object[functionName] = function () {
        const returnValue = originalFunction.apply(this, arguments);

        callback.apply(this, [returnValue, originalFunction, arguments]);

        return returnValue;
      };
    })(object[functionName]);
  };

  const queue = {
    waitingQueue: [],

    isRunning: false,

    execute: function (task) {
      task().finally(() => {
        if (this.waitingQueue.length) {
          const next = this.waitingQueue.shift();

          this.execute(next);
        } else {
          this.isRunning = false;
        }
      });
    },

    enqueue: function (task) {
      if (this.isRunning) {
        this.waitingQueue.push(task);
      } else {
        this.isRunning = true;

        this.execute(task);
      }
    },
  };

  const cache = {};

  const execute = (argid) => {
    queue.enqueue(async () => {
      const args = ui.postArg.data[argid];

      if (args.comment) return;

      const uid = +args.pAid;

      if (uid > 0) {
        const anchor = ui.postArg.data[argid].uInfoC.querySelector("[name=uid]")
          .parentNode;

        const handleLikes = (res) => {
          const matches = res.match(/{"type":8,"data":(\d+)}/);
          const value = matches ? matches[1] : 0;

          const element = document.createElement("SPAN");

          element.className =
            "small_colored_text_btn stxt block_txt_c2 vertmod";
          element.innerHTML = `<span class="white"><span style="font-family: comm_glyphs; -webkit-font-smoothing: antialiased; line-height: 1em;">⯅</span>&nbsp;${value}</span>`;

          anchor.append(element);
        };

        const handleFollows = (res) => {
          const matches = res.match(/"follow_by_num":(\d+)/);
          const value = matches ? matches[1] : 0;

          if (value) {
            const element = document.createElement("SPAN");

            element.className =
              "small_colored_text_btn stxt block_txt_c2 vertmod";
            element.innerHTML = `<span class="white"><span style="font-family: comm_glyphs; -webkit-font-smoothing: antialiased; line-height: 1em;">★</span>&nbsp;${value}</span>`;

            anchor.append(element);
          }
        };

        if (cache[uid] === undefined) {
          cache[uid] = await fetch(
            `/nuke.php?__lib=ucp&__act=get&lite=js&uid=${uid}`
          ).then((res) => res.text());
        }

        handleLikes(cache[uid]);
        handleFollows(cache[uid]);
      }
    });
  };

  hookFunction(ui, "postDisp", (returnValue, originalFunction, arguments) =>
    execute(arguments[0])
  );

  Object.keys(ui.postArg.data).forEach((i) => execute(i));
})(commonui);