hippie-view/partials/script-log.liquid
sthag 980b611a31 feat: Add current state from hippie main repo
- commit 86f3c679
- source/view/_includes/hippie
2026-04-23 20:56:04 +02:00

113 lines
No EOL
3.4 KiB
Text

<script>
// Entry script at page init
let debugOn = {{ hippie.debugMode | default: false, allow_false: true }};
let debugOnScreen = false;
let assetsLoaded = false;
// Get the current time difference between page
// load and when this func was invoked
function getTimeDiff() {
return new Date().getTime() - performance.timing.navigationStart;
}
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width
? n
: new Array(width - n.length + 1).join(z) + n;
}
// Log message to console and add
// performance measuring information
function logPerf(msg, arg) {
if (debugOn) {
if (debugOnScreen && assetsLoaded) {
if (!document.getElementById('jsLogPerf')) {
let wrap = document.createElement('div');
let box = document.createElement('div');
wrap.style.position = 'relative';
box.style.cssText = `position: fixed;
bottom: 16px;
right: 40px;
zIndex: 1000;
padding: 0 8px;
background: rgba(255,255,255,.6);
color: rgb(128);
fontSize: 1rem;`;
box.id = 'jsLogPerf';
wrap.prepend(box);
document
.body
.prepend(wrap);
/*
$('<div><div></div></div>')
.css('position', 'relative')
.find('div')
.attr('id', 'jsLogPerf')
.css({
position: 'fixed',
bottom: '16px',
right: '40px',
zIndex: '1000',
padding: '0 8px',
background: 'rgba(255,255,255,.6)',
color: 'rgb(128)',
fontSize: '1rem'
})
.end()
.prependTo('body');
*/
// $('body').prepend('<div style="position:relative;"></div>');
// $('div').first().prepend('<div id="jslogPerf" style="position:fixed;bottom:8px;right:16px;z-index:1000;padding:8px;background:rgb(0,255,255,.5)"></div>');
}
let code = document.createElement('code');
code.style.cssText = `display: block;
margin: 8px 0;
padding: 1px 4px;
background-color: transparent;
color: black;
font-size: 12px;
line-height: 1.1;`;
code.innerHTML = '<b>' + pad(getTimeDiff(), 5) + '</b>ms :: ' + msg + '';
document
.getElementById('jsLogPerf')
.append(code);
// document.getElementById('jsLogPerf').append('<p class="code_solo txt_smaller"><b>' + pad(getTimeDiff(), 5) + '</b>ms :: ' + msg + '</p>');
// $('#jsLogPerf').append('<p class="code_solo txt_smaller"><b>' + pad(getTimeDiff(), 5) + '</b>ms :: ' + msg + '</p>');
// NOTE: Alternative short-circuit evaluation
// needs element in document but prevents error if not
// $log = $log || $('#jslogPerf');
// $log.append('<p style="margin-bottom:4px;font-size:.75em;"><b>' + getTimeDiff() + '</b>ms :: ' + msg);
}
if (window.console) {
const time = pad(getTimeDiff(), 5) + 'ms :: ';
console.debug(time + msg, (
arg
? arg
: ''));
// NOTE: Non standard feature. Not available on IE
if (console.timeStamp) {
console.timeStamp(msg);
}
}
}
}
function logAdd(msg, listener) {
document.addEventListener(listener, function () {
logPerf(msg, listener);
});
}
</script>