High-res cover images on genius.com

Changes all cover images on genius.com lyrics pages to their highest possible resolution

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        High-res cover images on genius.com
// @description Changes all cover images on genius.com lyrics pages to their highest possible resolution
// @namespace   https://github.com/Sv443
// @version     1.0.3
// @author      Sv443
// @copyright   Sv443 (https://github.com/Sv443)
// @license     MIT
// @match       *://genius.com/*
// @homepageURL https://github.com/Sv443/High-res-cover-images-on-genius.com#readme
// @supportURL  https://github.com/Sv443/High-res-cover-images-on-genius.com/issues
// @grant       none
// @run-at      document-start
// ==/UserScript==

/** Regex to match the asset URL and extract the current width & height and the max width & height */
const imgSrcRegex = /https?:\/\/[a-zA-Z0-9]+\.genius\.com\/unsafe\/\d+x\d+\/.+\.(\d+)x(\d+)x\d+\.[a-z0-9]{3,}/;
/** Max amount of seconds to check for image elements before giving up */
const timeoutSec = 5;
/** Interval in milliseconds between checking for image elements */
const checkInterval = 100;

function run(startTs = Date.now()) {
  const imgs = document.querySelectorAll("#application img");
  if(imgs && imgs.length > 0) {
    let replacedAmt = 0;
    for(const img of imgs) {
      const matches = "src" in img ? img.src.match(imgSrcRegex) : null;
      if(matches && matches.length === 3) {
        const [src, maxW, maxH] = matches;
        img.src = src.replace(/\/unsafe\/\d+x\d+\//, `/unsafe/${maxW}x${maxH}/`);
        replacedAmt++;
      }
    }
    if(replacedAmt > 0)
      console.info(`Replaced ${replacedAmt} low-res ${replacedAmt === 1 ? "image with a high-res one" : "images with high-res ones"}.\n\n${GM.info.script.name} v${GM.info.script.version}\n${GM.info.script.homepageURL}`);
    else
      warn("Found no suitable images to replace.");
  }
  else if(Date.now() - startTs < timeoutSec * 1000)
    setTimeout(() => run(startTs), checkInterval);
  else
    warn(`Couldn't find any images to replace after ${timeoutSec} seconds.`);
}

function warn(msg) {
  console.warn(`${msg}\n\nIf you are not on a lyrics page, you can ignore this warning.\nIf this issue keeps happening on lyrics pages, please submit a bug report here: ${GM.info.script.supportURL}`);
}

document.addEventListener("DOMContentLoaded", () => run());