PWA Installation Prompt

Prompt users to install the PWA on all websites

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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);
        });
    }
})();