Files
dokuwiki-plugin-botmon/client.js

76 lines
2.2 KiB
JavaScript
Raw Normal View History

2025-09-01 16:25:25 +02:00
botmon_client = {
init: function() {
/* send the page view request: */
2025-08-20 22:58:42 +03:00
this._onPageView(this._src.replace( this._scriptName, '/pview.php'));
/* send the first heartbeat signal after x seconds: */
2025-08-20 22:58:42 +03:00
setTimeout(this._onHeartbeat.bind(this, this._src.replace( this._scriptName, '/tick.php')),this._heartbeat * 1000);
},
/* keep a reference to the script URL: */
_src: document.currentScript.src,
/* heartbeat signal every x seconds: */
_heartbeat: 30,
/* name of this script (with slash): */
_scriptName: '/client.js',
/* function to init page data on server: */
_onPageView: async function(url) {
try {
/* collect the data to send: */
const visit = {
'pg': JSINFO.id,
2025-09-01 16:25:25 +02:00
'u': document._botmon.user || null,
'lg': navigator.language,
2025-09-01 16:25:25 +02:00
'lt': ( document._botmon ? Date.now() - document._botmon.t0 : null),
2025-08-30 13:01:50 +03:00
'r': document.referrer /*,
'tz': new Date().getTimezoneOffset(),
'url': window.location.href,
'scr': screen.width+':'+screen.height,
'l': navigator.languages */
}
/* compile to a FormData object: */
const data = new FormData();
2025-08-20 22:58:42 +03:00
data.append( "pageview", JSON.stringify( visit ) );
/* send the request */
const response = await fetch(url + '?t=' + Date.now(), {
method: 'POST',
body: data
});
if (!response.ok) {
throw new Error(response.status + ' ' + response.statusText + ' - ' + url);
}
} catch (err) {
console.error('Error: ', err);
}
},
/* function to call regularly to show the user is still on the page: */
_onHeartbeat: async function(url) {
2025-09-01 16:25:25 +02:00
//console.info('botmon_client._onHeartbeat', url);
2025-08-20 22:58:42 +03:00
2025-09-04 09:44:01 +02:00
let uid = document._botmon.user || null;
try {
2025-09-04 09:44:01 +02:00
const response = await fetch(url + '?p=' + encodeURIComponent(JSINFO.id) + '&t=' + Date.now() + ( uid ? '&u=' + encodeURIComponent(uid) : ''), {
method: 'HEAD'
});
if (!response.ok) {
throw new Error(response.status + ' ' + response.statusText + ' - ' + url);
}
} catch (err) {
console.error(err);
} finally {
/* send the next heartbeat signal after x seconds: */
2025-08-21 08:34:47 +03:00
setTimeout(this._onHeartbeat.bind(this, this._src.replace( this._scriptName, '/tick.php')),this._heartbeat * 1000);
}
}
}
// init the script:
2025-09-01 16:25:25 +02:00
botmon_client.init();