PS Store Subscription Links Locale Fix

This script fixes the issue that the PS Plus and EA Play links on the PS Store subscriptions page do not have a locale set.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         PS Store Subscription Links Locale Fix
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  This script fixes the issue that the PS Plus and EA Play links on the PS Store subscriptions page do not have a locale set.
// @author       Nathaniel Wu
// @include      *store.playstation.com/*
// @license      Apache-2.0
// @supportURL   https://gist.github.com/Nathaniel-Wu/2ff7fe939acca362d7fdeaf17b4f0d18
// @grant        none
// ==/UserScript==

(function () {
    'use strict';
    const in_iframe = () => {
        try {
            return window.self !== window.top;
        } catch (e) {
            return true;
        }
    }
    if (!in_iframe()) {
        const onSubscriptionPage = () => {
            return /^https?:\/\/store\.playstation\.com\/[^\/]+\/pages\/subscriptions($|\/)/g.test(window.location.href) || /^https?:\/\/store\.playstation\.com\/[^\/]*\/view\/[^\/]+\/[^\/]+\/?$/.test(window.location.href);
        };
        const fixSubscriptionLinks = () => {
            const locale = window.location.href.replace(/^https?:\/\/store\.playstation\.com\/([^\/]+)\/(.+)?$/g, '$1');
            document.querySelectorAll('.psw-solid-link.psw-button.psw-primary-button.psw-solid-button').forEach(e => {
                if (/(\/|\.)playstation\.com\/ps-plus($|\/)/g.test(e.href)) {
                    // Fix the PS Plus link
                    e.href = e.href.replace(/((\/|\.)playstation\.com\/)ps-plus/g, `$1${locale}/ps-plus`);
                } else if (/(\/|\.)playstation\.com\/eaplay($|\/)/g.test(e.href)) {
                    // Fix the EA Play link
                    e.href = e.href.replace(/((\/|\.)playstation\.com\/)eaplay/g, `$1${locale}/games/ea-play`);
                } else if (/(\/|\.)playstation\.com\/games\/ubisoft-plus-classics($|\/)/g.test(e.href)) {
                    // Fix the Ubisoft Plus Classics link
                    e.href = e.href.replace(/((\/|\.)playstation\.com\/)games\/ubisoft-plus-classics/g, `$1${locale}/games/ubisoft-plus-classics`);
                }
            });
        };
        if (onSubscriptionPage())
            fixSubscriptionLinks()
        let activeTransitions = 0;
        document.addEventListener('transitionend', (e) => {
            activeTransitions++;
            setTimeout(() => {
                activeTransitions--;
                if (activeTransitions == 0 && onSubscriptionPage())
                    fixSubscriptionLinks();
            }, 200);
        });
    }
})();