PWA Installation Prompt

Prompt users to install the PWA on all websites

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         PWA Installation Prompt
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Prompt users to install the PWA on all websites
// @author       YourName
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let deferredPrompt;

    window.addEventListener('beforeinstallprompt', (e) => {
        // Prevent the mini-infobar from appearing on mobile
        e.preventDefault();
        // Stash the event so it can be triggered later
        deferredPrompt = e;

        // Create and show the custom install prompt
        showInstallPrompt();
    });

    function showInstallPrompt() {
        if (!deferredPrompt) return;

        // Create a button to trigger the PWA install
        const installBtn = document.createElement('button');
        installBtn.textContent = 'Install this PWA';
        installBtn.style.position = 'fixed';
        installBtn.style.bottom = '20px';
        installBtn.style.right = '20px';
        installBtn.style.padding = '10px 20px';
        installBtn.style.zIndex = '1000';
        installBtn.style.backgroundColor = '#0073e6';
        installBtn.style.color = 'white';
        installBtn.style.border = 'none';
        installBtn.style.cursor = 'pointer';
        installBtn.style.borderRadius = '5px';
        installBtn.style.boxShadow = '0 2px 10px rgba(0, 0, 0, 0.2)';

        document.body.appendChild(installBtn);

        installBtn.addEventListener('click', async () => {
            // Show the install prompt
            deferredPrompt.prompt();
            // Wait for the user to respond to the prompt
            const { outcome } = await deferredPrompt.userChoice;
            if (outcome === 'accepted') {
                console.log('User accepted the PWA installation prompt');
            } else {
                console.log('User dismissed the PWA installation prompt');
            }
            // Clear the deferred prompt
            deferredPrompt = null;
            document.body.removeChild(installBtn);
        });
    }
})();