PlayStore Redirect Buttons

Adds multiple buttons to redirect from Google PlayStore to different websites, even on error pages.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         PlayStore Redirect Buttons
// @namespace    PlayStore Redirect Buttons
// @version      1.2
// @description  Adds multiple buttons to redirect from Google PlayStore to different websites, even on error pages.
// @author       Rishabh
// @match        https://play.google.com/store/apps/details?id=*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=play.google.com
// @grant        GM_addStyle
// ==/UserScript==

(function () {
    'use strict';

    function getPackageId() {
        const m = location.href.match(/[?&]id=([a-zA-Z0-9._-]+)/);
        return m ? m[1] : null;
    }

    function addCSS(shadowRoot) {
        const style = document.createElement("style");
        style.textContent = `
            .mr-buttons-container {
                display: flex;
                flex-wrap: wrap;
                gap: 4px;
                margin-top: 8px;
            }
            .mr-button {
                color: #fff;
                background: #555;
                font-family: Roboto, Arial, sans-serif;
                font-size: .9rem;
                font-weight: 500;
                height: 36px;
                padding: 0 12px;
                border-radius: 8px;
                display: inline-flex;
                align-items: center;
                justify-content: center;
                box-sizing: border-box;
                transition: transform 0.2s, filter 0.2s;
                text-decoration: none;
                cursor: pointer;
            }
            .mr-button:hover {
                filter: brightness(0.85);
                transform: translateY(-2px);
            }
        `;
        shadowRoot.appendChild(style);
    }

    function findTargetButton() {
        const buttons = Array.from(document.querySelectorAll('button'));
        return buttons.find(btn => {
            const text = btn.innerText.trim();
            return text === "Install" ||
                   text === "Install on more devices" ||
                   text.endsWith("Buy");
        });
    }

    function isErrorPage() {
        return document.body.innerText.includes("We're sorry, the requested URL was not found on this server.");
    }

    function insertButtons(pkgId) {
        if (!pkgId) return;
        if (document.querySelector(".mr-buttons-host")) return;

        let parent;
        if (isErrorPage()) {
            parent = document.querySelector("body > div") || document.body;
        } else {
            const targetBtn = findTargetButton();
            parent = targetBtn?.closest('div');
            if (!parent) {
                const title = document.querySelector("h1 span");
                parent = title?.parentElement;
            }
        }
        if (!parent) return;

        const host = document.createElement("div");
        host.className = "mr-buttons-host";
        parent.appendChild(host);

        const shadow = host.attachShadow({ mode: "open" });
        addCSS(shadow);

        const container = document.createElement("div");
        container.className = "mr-buttons-container";
        shadow.appendChild(container);

        const buttons = [
            { text: 'A2zapk History', url: `https://a2zapk.io/History/${pkgId}/`, color: '#00875f' },
            { text: 'A2zapk Download', url: `https://a2zapk.io/apk/${pkgId}.html`, color: '#00875f' },
            { text: 'APKMirror',      url: `https://www.apkmirror.com/?post_type=app_release&searchtype=apk&s=${pkgId}`, color: '#FF8B14' },
            { text: 'APKPure',        url: `https://apkpure.com/search?q=${pkgId}`, color: '#24cd77' },
            { text: 'APKCombo',       url: `https://apkcombo.com/search?q=${pkgId}`, color: '#286090' }
        ];

        for (const b of buttons) {
            const link = document.createElement("a");
            link.textContent = b.text;
            link.href = b.url;
            link.className = "mr-button";
            link.style.backgroundColor = b.color;
            container.appendChild(link);
        }
    }

    function ensureButtons() {
        const pkg = getPackageId();
        insertButtons(pkg);
    }

    const observer = new MutationObserver(() => {
        ensureButtons();
    });
    observer.observe(document.body, { childList: true, subtree: true });

    ensureButtons();

    let lastPkg = getPackageId();
    setInterval(() => {
        const pkg = getPackageId();
        if (pkg && lastPkg && pkg !== lastPkg) {
            location.reload();
        }
        lastPkg = pkg || lastPkg;
    }, 1000);
})();