UWP App Jump to Download

Insert a custom link before the first child element on specific app pages, open the download page

// ==UserScript==
// @name         UWP App Jump to Download
// @version      0.0.5
// @description  Insert a custom link before the first child element on specific app pages, open the download page
// @author       aspen138
// @match        *://apps.microsoft.com/detail/*
// @namespace    tampermonkey
// @license      MIT
// @grant        none
// @grant        GM_openInTab
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        window.focus
// ==/UserScript==


// test case: https://apps.microsoft.com/detail/9nt1r1c2hh7j?hl=en-us&gl=US



const openDownloadPage= () =>{
    'use strict';

    // Check if we're on the correct page
    if (!window.location.href.includes('/detail/')) return;

    const appUrl = window.location.href;

    // Function to submit form to store.rg-adguard.net
    const submitForm = () => {
        const form = document.createElement('form');
        form.method = 'POST';
        form.action = 'https://store.rg-adguard.net/api/GetFiles';
        form.target = '_blank';

        // Create input elements
        const inputs = [
            { name: 'type', value: 'url' },
            { name: 'url', value: appUrl },
            { name: 'ring', value: 'Retail' },
            { name: 'lang', value: 'en-US' },
        ];

        inputs.forEach(({ name, value }) => {
            const input = document.createElement('input');
            input.type = 'hidden';
            input.name = name;
            input.value = value;
            form.appendChild(input);
        });

        document.body.appendChild(form);
        form.submit();
        document.body.removeChild(form);
    };

    // Automatically submit the form on page load
    submitForm();

};

(function () {
    'use strict';

    // Check if we're on the correct page
    if (!window.location.href.includes('/detail/')) return;

    const appUrl = window.location.href;

    // Create the banner element
    const banner = document.createElement('div');
    banner.style.cssText = `
        background-color: #f44336;
        color: white;
        font-size: 16px;
        padding: 10px;
        text-align: center;
        cursor: pointer;
        border-bottom: 2px solid #d32f2f;
        position: sticky;
        top: 0;
        z-index: 1000;
    `;
    banner.textContent = 'Click here to open Download page of this UWP App';

    // Function to open the new page and auto-fill the input
    const openNewTab = () => {
        const newTab = window.open('https://store.rg-adguard.net/', '_blank');
        if (newTab) {
            // Inject the script into the new tab after it loads
            newTab.onload = () => {
                const inputElement = newTab.document.getElementById('url');
                console.log("inputElement=",inputElement);
                if (inputElement) {
                    inputElement.value = appUrl; // Set the value
                    inputElement.placeholder = appUrl; // Update the placeholder
                }

                const button = newTab.document.querySelector('input[type="button"]');
                if (button) button.click();
            };
        }
    };

    banner.onclick = openDownloadPage;

    // Insert the banner at the top of the page
    const firstElement = document.body.firstChild;
    document.body.insertBefore(banner, firstElement);

    // Automatically open the new tab on page load
    let autoOpenNewTab=false;
    if(autoOpenNewTab) openNewTab();

})();