Reddit Title Upvote Ratio Tooltip

On a Reddit post page, show upvote_ratio (%) as a tooltip on the post title.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Reddit Title Upvote Ratio Tooltip
// @namespace    https://greasyfork.org/en/users/1479408
// @license MIT
// @version      1.0
// @description  On a Reddit post page, show upvote_ratio (%) as a tooltip on the post title.
// @match        https://www.reddit.com/*/*/comments/*
// @grant        GM.xmlHttpRequest
// @connect      reddit.com
// @run-at       document-idle
// ==/UserScript==

(function() {
  'use strict';

  // Run once on page load
  const titleEl = document.querySelector('h1[id^="post-title-t3_"]');
  if (!titleEl) return;

  // Extract the base‑36 post ID from the element's id attribute
  const [, postId] = titleEl.id.split('t3_');
  if (!postId) return;

  // Fetch JSON and set tooltip to upvote_ratio × 100
  GM.xmlHttpRequest({
    method: "GET",
    url: `https://www.reddit.com/comments/${postId}.json?raw_json=1`,
    responseType: 'json',
    onload: resp => {
      try {
        const ratio = resp.response[0].data.children[0].data.upvote_ratio;
        titleEl.title = ratio != null
          ? `${(ratio * 100).toFixed(1)}%`
          : 'N/A';
      } catch (e) {
        console.error('TitleUpPctTooltip error', e);
      }
    }
  });
})();