Links for Game Editions in PlayStation Store

Add hyperlink to the other editions in playstation store

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Links for Game Editions in PlayStation Store
// @namespace    https://github.com/rod24574575
// @description  Add hyperlink to the other editions in playstation store
// @version      1.1.0
// @license      MIT
// @author       rod24574575
// @homepage     https://github.com/rod24574575/PsStoreEditionLink
// @homepageURL  https://github.com/rod24574575/PsStoreEditionLink
// @supportURL   https://github.com/rod24574575/PsStoreEditionLink/issues
// @match        *://store.playstation.com/*/product/*
// @match        *://store.playstation.com/*/concept/*
// @match        *://www.playstation.com/*/games/*
// @run-at       document-idle
// ==/UserScript==

// @ts-check
'use strict';

(function() {
  function run() {
    /**
     * @param {string} trackString
     * @returns {string | null}
     */
    function findSkuId(trackString) {
      /** @type {string} */
      let rawSku;
      try {
        const track = JSON.parse(trackString);
        if (!track || typeof track !== 'object') {
          return null;
        }

        const sku = track?.attributes?.sku;
        if (typeof sku !== 'string') {
          return null;
        }

        rawSku = sku;
      } catch {
        return null;
      }

      const lastDashIndex = rawSku.lastIndexOf('-');
      if (lastDashIndex < 0) {
        return null;
      }

      return rawSku.slice(0, lastDashIndex);
    }

    /**
     * @param {string} id
     * @returns {string}
     */
    function createProductUrl(id) {
      const match = location.href.match(/\.playstation\.com\/([^/]+)\//i);
      if (!match) {
        return '';
      }
      const locale = match[1];
      return `${location.protocol}//store.playstation.com/${locale}/product/${id}`;
    }

    for (const productEditionEl of document.querySelectorAll('article[data-qa^="mfeUpsell#productEdition"]')) {
      const editorTitleEl = productEditionEl.querySelector('[data-qa*="editionName"]')?.parentElement;
      if (!editorTitleEl) {
        continue;
      }

      const parent = editorTitleEl.parentElement;
      if (!parent) {
        continue;
      }

      const addCartEl = productEditionEl.querySelector('button[data-track]');
      if (!addCartEl) {
        continue;
      }

      const skuId = findSkuId(addCartEl.getAttribute('data-track') ?? '');
      if (!skuId) {
        continue;
      }

      const anchor = document.createElement('a');
      anchor.href = createProductUrl(skuId);
      Object.assign(anchor.style, {
        display: 'block',
      });

      parent.insertBefore(anchor, editorTitleEl);
      anchor.appendChild(editorTitleEl);
    }
  }

  /** @type {number | undefined} */
  let timer = window.setInterval(() => {
    if (!document.querySelector('[data-qa="mfeUpsell"]:not([data-reactroot])')) {
      return;
    }

    run();
    window.clearInterval(timer);
    timer = undefined;
  }, 100);
})();