App Store BundleID Viewer (with Copy Button)

Display the bundleID on Apple App Store preview pages with a Copy button

目前為 2025-05-23 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         App Store BundleID Viewer (with Copy Button)
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Display the bundleID on Apple App Store preview pages with a Copy button
// @author       sharmanhall
// @match        https://apps.apple.com/*/app/*/id*
// @icon         https://www.apple.com/favicon.ico
// @grant        GM_setClipboard
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    const MAX_ATTEMPTS = 10;
    const RETRY_INTERVAL = 1000; // ms

    function waitForPageReady(attempt = 0) {
        if (document.body) {
            getBundleID();
        } else if (attempt < MAX_ATTEMPTS) {
            setTimeout(() => waitForPageReady(attempt + 1), RETRY_INTERVAL);
        } else {
            console.error('[BundleID Viewer] Failed to load body after multiple attempts.');
        }
    }

    async function getBundleID() {
        const appIdMatch = window.location.href.match(/id(\d+)/);
        if (!appIdMatch) {
            console.warn('[BundleID Viewer] App ID not found in URL.');
            return;
        }

        const appId = appIdMatch[1];
        const lookupUrl = `https://itunes.apple.com/lookup?id=${appId}`;

        try {
            const response = await fetch(lookupUrl);
            const data = await response.json();

            if (!data.results || !data.results[0] || !data.results[0].bundleId) {
                console.warn('[BundleID Viewer] Bundle ID not found in API response.');
                return;
            }

            const bundleId = data.results[0].bundleId;
            console.log('[BundleID Viewer] Found Bundle ID:', bundleId);
            showBundleID(bundleId);
        } catch (err) {
            console.error('[BundleID Viewer] Error fetching bundle ID:', err);
        }
    }

    function showBundleID(bundleId) {
        const wrapper = document.createElement('div');
        wrapper.style.position = 'fixed';
        wrapper.style.bottom = '20px';
        wrapper.style.right = '20px';
        wrapper.style.backgroundColor = '#000';
        wrapper.style.color = '#fff';
        wrapper.style.padding = '10px 14px';
        wrapper.style.borderRadius = '8px';
        wrapper.style.boxShadow = '0 0 12px rgba(0,0,0,0.4)';
        wrapper.style.zIndex = '99999';
        wrapper.style.fontFamily = 'monospace';
        wrapper.style.display = 'flex';
        wrapper.style.alignItems = 'center';
        wrapper.style.gap = '8px';

        const text = document.createElement('span');
        text.textContent = `📦 Bundle ID: ${bundleId}`;

        const button = document.createElement('button');
        button.textContent = 'Copy';
        button.style.background = '#1E88E5';
        button.style.color = 'white';
        button.style.border = 'none';
        button.style.padding = '4px 8px';
        button.style.cursor = 'pointer';
        button.style.borderRadius = '4px';
        button.style.fontSize = '12px';

        button.onclick = () => {
            if (typeof GM_setClipboard === 'function') {
                GM_setClipboard(bundleId);
            } else {
                navigator.clipboard.writeText(bundleId).catch(err =>
                    alert('Clipboard copy failed: ' + err)
                );
            }
            button.textContent = 'Copied!';
            setTimeout(() => (button.textContent = 'Copy'), 1500);
        };

        wrapper.appendChild(text);
        wrapper.appendChild(button);
        document.body.appendChild(wrapper);
    }

    window.addEventListener('load', () => waitForPageReady());
})();