<?php
/*
 * Shared PWA footer scripts — include this right BEFORE </body> on EVERY page.
 *   1. registers the service worker
 *   2. keeps internal link taps INSIDE the standalone home-screen app on iOS,
 *      which is what stops the in-app Safari browser (X + address bar + toolbar)
 *      from popping up when the user navigates between pages.
 */
?>
<script>
if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/seven-credit-system/service-worker.js', { scope: '/seven-credit-system/' });
}

/* Keep same-site navigation inside the standalone (home-screen) app on iOS.
   Without this, iOS opens links in the in-app browser chrome even when the
   PWA meta tags are present. Links with target="_blank" or a download attr
   are left alone so external links / file downloads still work normally. */
(function () {
    if (('standalone' in navigator) && navigator.standalone) {
        document.addEventListener('click', function (e) {
            var a = e.target.closest('a');
            if (a && a.href &&
                a.host === location.host &&
                !a.target &&
                !a.hasAttribute('download') &&
                a.getAttribute('href').indexOf('#') !== 0) {
                e.preventDefault();
                location.href = a.href;
            }
        }, false);
    }
})();
</script>