25 lines
2.2 KiB
JavaScript
25 lines
2.2 KiB
JavaScript
// Android/Capacitor-Bruecke: Back-Button, Statusleiste, Haptik bei Level-up, Audio-Unlock. Im Browser no-op.
|
|
(function(){
|
|
var C = window.Capacitor; if (!window.CK_IS_NATIVE || !C) return;
|
|
var P = C.Plugins || {};
|
|
try { P.StatusBar && P.StatusBar.setStyle({style:'DARK'}); P.StatusBar && P.StatusBar.setBackgroundColor({color:'#0f0f1a'}); } catch(e){}
|
|
try { P.SplashScreen && setTimeout(function(){ P.SplashScreen.hide(); }, 300); } catch(e){}
|
|
if (P.App) P.App.addListener('backButton', function(){
|
|
var open = Array.prototype.slice.call(document.querySelectorAll('dialog[open], .modal.open, .modal:not([hidden]), [role="dialog"]:not([hidden])'))
|
|
.filter(function(el){ return el.offsetParent !== null || el.tagName === 'DIALOG'; });
|
|
if (open.length){ var el = open[open.length-1]; if (el.close) el.close(); else { var x = el.querySelector('[data-close], .close, button[aria-label*="chlie"]'); if (x) x.click(); else el.hidden = true; } return; }
|
|
if (history.length > 1 && location.hash) { history.back(); return; }
|
|
P.App.exitApp();
|
|
});
|
|
// Web Audio nach erster Beruehrung freischalten
|
|
function unlock(){ try { var Ctx = window.AudioContext || window.webkitAudioContext; if (window.CK_AUDIO_CTX && window.CK_AUDIO_CTX.resume) window.CK_AUDIO_CTX.resume(); else if (Ctx){ var c = new Ctx(); var b = c.createBuffer(1,1,22050); var s = c.createBufferSource(); s.buffer=b; s.connect(c.destination); s.start(0); c.resume && c.resume(); } } catch(e){} }
|
|
document.addEventListener('touchstart', unlock, {once:true, passive:true});
|
|
document.addEventListener('pointerdown', unlock, {once:true});
|
|
// Haptik bei Level-up: #lvl beobachten
|
|
function buzz(){ try { if (P.Haptics) P.Haptics.notification({type:'SUCCESS'}); else if (navigator.vibrate) navigator.vibrate([40,30,80]); } catch(e){} }
|
|
window.CK_HAPTIC = buzz;
|
|
document.addEventListener('DOMContentLoaded', function(){
|
|
var el = document.getElementById('lvl'); if (!el) return; var last = el.textContent;
|
|
new MutationObserver(function(){ var n = parseInt((el.textContent.match(/\d+/)||[0])[0],10), o = parseInt((last.match(/\d+/)||[0])[0],10); if (o && n > o) buzz(); last = el.textContent; }).observe(el, {childList:true, characterData:true, subtree:true});
|
|
});
|
|
})();
|