Untitled

 avatar
unknown
plain_text
5 months ago
2.6 kB
2
Indexable
(function () {
    // Detect Instagram WebView
    function isInstagramWebView() {
        let ua = navigator.userAgent || navigator.vendor || window.opera;
        return ua.includes("Instagram");
    }

    // Show a message and provide a button to open in native browser
    function showNativeBrowserPrompt() {
        // Create a modal or overlay
        const overlay = document.createElement("div");
        overlay.style.position = "fixed";
        overlay.style.top = "0";
        overlay.style.left = "0";
        overlay.style.width = "100%";
        overlay.style.height = "100%";
        overlay.style.backgroundColor = "rgba(0, 0, 0, 0.8)";
        overlay.style.zIndex = "10000";
        overlay.style.display = "flex";
        overlay.style.justifyContent = "center";
        overlay.style.alignItems = "center";
        overlay.style.color = "#fff";
        overlay.style.textAlign = "center";

        // Create the message and button
        const content = document.createElement("div");
        content.innerHTML = `
            <p>You are viewing this page in Instagram's browser. For a better experience, please open it in your native browser.</p>
            <button id="open-native-browser" style="padding: 10px 20px; background: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer;">
                Open in Browser
            </button>
        `;
        overlay.appendChild(content);

        // Append the overlay to the body
        document.body.appendChild(overlay);

        // Handle the button click
        const openButton = document.getElementById("open-native-browser");
        openButton.addEventListener("click", () => {
            let url = window.location.href;

            // For Android: Try to open in Chrome
            if (/android/i.test(navigator.userAgent)) {
                window.location.href = `intent://${url.replace(/^https?:\/\//, '')}#Intent;scheme=https;package=com.android.chrome;end;`;
            }
            // For iOS: Try to open in Chrome, fallback to Safari
            else if (/iPhone|iPad|iPod/i.test(navigator.userAgent)) {
                const chromeUrl = `googlechrome://${url.replace(/^https?:\/\//, '')}`;
                window.open(chromeUrl, "_blank");

                // Fallback to Safari if Chrome is not installed
                setTimeout(() => {
                    window.location.href = url;
                }, 500);
            }
        });
    }

    // Run only if inside Instagram WebView
    if (isInstagramWebView()) {
        setTimeout(showNativeBrowserPrompt, 1000); // Delay for better execution
    }
})();
Editor is loading...
Leave a Comment