Added sitestat and tag to most pages.

This commit is contained in:
Andrew Lalis 2023-10-04 21:08:23 -04:00
parent 44f6e7f013
commit b16b07bfa8
14 changed files with 117 additions and 0 deletions

View File

@ -11,6 +11,7 @@
<script src="scripts/themes.min.js"></script> <script src="scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript> <noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="styles/style.css" type="text/css"> <link rel="stylesheet" href="styles/style.css" type="text/css">
<script src="scripts/sitestat.js?remote-url=sitestat.andrewlalis.com" async></script>
</head> </head>
<body> <body>

View File

@ -11,6 +11,7 @@
<script src="../scripts/themes.min.js"></script> <script src="../scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript> <noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="../styles/style.css" type="text/css"> <link rel="stylesheet" href="../styles/style.css" type="text/css">
<script src="../scripts/sitestat.js?remote-url=sitestat.andrewlalis.com" async></script>
<link rel="stylesheet" href="../styles/article.css" type="text/css"/> <link rel="stylesheet" href="../styles/article.css" type="text/css"/>
<link rel="stylesheet" href="../vendor/prism.css" type="text/css"/> <link rel="stylesheet" href="../vendor/prism.css" type="text/css"/>

View File

@ -11,6 +11,7 @@
<script src="../scripts/themes.min.js"></script> <script src="../scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript> <noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="../styles/style.css" type="text/css"> <link rel="stylesheet" href="../styles/style.css" type="text/css">
<script src="../scripts/sitestat.js?remote-url=sitestat.andrewlalis.com" async></script>
<link rel="stylesheet" href="../styles/article.css" type="text/css"> <link rel="stylesheet" href="../styles/article.css" type="text/css">
<link rel="stylesheet" href="../vendor/prism.css" type="text/css"/> <link rel="stylesheet" href="../vendor/prism.css" type="text/css"/>

View File

@ -11,6 +11,7 @@
<script src="../scripts/themes.min.js"></script> <script src="../scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript> <noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="../styles/style.css" type="text/css"> <link rel="stylesheet" href="../styles/style.css" type="text/css">
<script src="../scripts/sitestat.js?remote-url=sitestat.andrewlalis.com" async></script>
<link rel="stylesheet" href="../styles/article.css" type="text/css"> <link rel="stylesheet" href="../styles/article.css" type="text/css">
<link rel="stylesheet" href="../vendor/prism.css" type="text/css"/> <link rel="stylesheet" href="../vendor/prism.css" type="text/css"/>

View File

@ -11,6 +11,7 @@
<script src="../scripts/themes.min.js"></script> <script src="../scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript> <noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="../styles/style.css" type="text/css"> <link rel="stylesheet" href="../styles/style.css" type="text/css">
<script src="../scripts/sitestat.js?remote-url=sitestat.andrewlalis.com" async></script>
<link rel="stylesheet" href="../styles/article.css" type="text/css"> <link rel="stylesheet" href="../styles/article.css" type="text/css">
<link rel="stylesheet" href="../vendor/prism.css" type="text/css"/> <link rel="stylesheet" href="../vendor/prism.css" type="text/css"/>

View File

@ -11,6 +11,7 @@
<script src="../scripts/themes.min.js"></script> <script src="../scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript> <noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="../styles/style.css" type="text/css"> <link rel="stylesheet" href="../styles/style.css" type="text/css">
<script src="../scripts/sitestat.js?remote-url=sitestat.andrewlalis.com" async></script>
<link rel="stylesheet" href="../styles/article.css" type="text/css"> <link rel="stylesheet" href="../styles/article.css" type="text/css">
<link rel="stylesheet" href="../vendor/prism.css" type="text/css"/> <link rel="stylesheet" href="../vendor/prism.css" type="text/css"/>

View File

@ -11,6 +11,7 @@
<script src="../scripts/themes.min.js"></script> <script src="../scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript> <noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="../styles/style.css" type="text/css"> <link rel="stylesheet" href="../styles/style.css" type="text/css">
<script src="../scripts/sitestat.js?remote-url=sitestat.andrewlalis.com" async></script>
<link rel="stylesheet" href="../styles/article.css" type="text/css"> <link rel="stylesheet" href="../styles/article.css" type="text/css">
<link rel="stylesheet" href="../vendor/prism.css" type="text/css"/> <link rel="stylesheet" href="../vendor/prism.css" type="text/css"/>

View File

@ -11,6 +11,7 @@
<script src="../scripts/themes.min.js"></script> <script src="../scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript> <noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="../styles/style.css" type="text/css"> <link rel="stylesheet" href="../styles/style.css" type="text/css">
<script src="../scripts/sitestat.js?remote-url=sitestat.andrewlalis.com" async></script>
<link rel="stylesheet" href="../styles/article.css" type="text/css"/> <link rel="stylesheet" href="../styles/article.css" type="text/css"/>
<link rel="stylesheet" href="../vendor/prism.css" type="text/css"/> <link rel="stylesheet" href="../vendor/prism.css" type="text/css"/>

View File

@ -11,6 +11,7 @@
<script src="scripts/themes.min.js"></script> <script src="scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript> <noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="styles/style.css" type="text/css"> <link rel="stylesheet" href="styles/style.css" type="text/css">
<script src="scripts/sitestat.js?remote-url=sitestat.andrewlalis.com" async></script>
</head> </head>
<body> <body>

View File

@ -11,6 +11,7 @@
<script src="scripts/themes.min.js"></script> <script src="scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript> <noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="styles/style.css" type="text/css"> <link rel="stylesheet" href="styles/style.css" type="text/css">
<script src="scripts/sitestat.js?remote-url=sitestat.andrewlalis.com" async></script>
</head> </head>
<body> <body>

View File

@ -11,6 +11,7 @@
<script src="scripts/themes.min.js"></script> <script src="scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript> <noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="styles/style.css" type="text/css"> <link rel="stylesheet" href="styles/style.css" type="text/css">
<script src="scripts/sitestat.js?remote-url=sitestat.andrewlalis.com" async></script>
<link rel="stylesheet" href="styles/logbook.css" type="text/css"> <link rel="stylesheet" href="styles/logbook.css" type="text/css">
</head> </head>

View File

@ -11,6 +11,7 @@
<script src="scripts/themes.min.js"></script> <script src="scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript> <noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="styles/style.css" type="text/css"> <link rel="stylesheet" href="styles/style.css" type="text/css">
<script src="scripts/sitestat.js?remote-url=sitestat.andrewlalis.com" async></script>
<link rel="stylesheet" href="styles/project.css" type="text/css"> <link rel="stylesheet" href="styles/project.css" type="text/css">
<link rel="stylesheet" href="vendor/prism.css" data-noprefix> <link rel="stylesheet" href="vendor/prism.css" data-noprefix>

104
scripts/sitestat.js Normal file
View File

@ -0,0 +1,104 @@
/*
sitestat.js is meant to be included in the <head> like so:
<script src="path/to/sitestat.js" async></script>
See test-site/index.html for an example.
*/
/**
* The global websocket singleton. It's initialized at the bottom of this script.
* @type {WebSocket | null}
*/
let WS = null;
let REMOTE_URL = null;
const RETRY_TIMEOUT = 1000;
let RETRY_COUNT = 0;
/**
* Sends some JSON object to sitestat.
* @param {object} data The data to send.
*/
function sendStat(data) {
if (WS !== null && WS.readyState === WebSocket.OPEN) {
WS.send(JSON.stringify(data));
} else {
console.warn("Couldn't send sitestat data because websocket is not open: ", data);
}
}
/**
* Handles any event encountered, and sends a small message to sitestat about it.
* @param {Event} event The event that occurred.
*/
function handleEvent(event) {
sendStat({
type: "event",
event: event.type
});
}
/**
* Gets the remote URL that sitestat is running at, from the query params of
* the script's `src` attribute. Throws an error if such a URL could not be
* found.
* @returns {string} The remote URL to connect to.
*/
function getRemoteUrl() {
const scriptUrl = document.currentScript.src;
const paramsIdx = scriptUrl.indexOf("?");
if (paramsIdx !== -1) {
const paramsStr = scriptUrl.substring(paramsIdx);
const params = new URLSearchParams(paramsStr);
const remoteUrl = params.get("remote-url");
if (remoteUrl !== null) {
return remoteUrl;
}
}
throw new Error("Missing `remote-url=...` query parameter on script src attribute.")
}
function initWS() {
console.info("Trying to open a connection to sitestat...");
WS = new WebSocket(`wss://${REMOTE_URL}/ws`);
WS.onopen = () => {
console.info(
"📈 Established a connection to %csitestat%c for %cnon-intrusive, non-identifiable%csite analytics.\nLearn more here: https://github.com/andrewlalis/sitestat or check your browser's network tab to see what's being sent.",
"font-weight: bold; font-style: italic; font-size: large; color: #32a852; background-color: #2e2e2e; padding: 5px;",
"",
"font-style: italic;"
);
sendStat({
type: "ident",
href: window.location.href,
userAgent: window.navigator.userAgent,
viewport: {
width: window.innerWidth,
height: window.innerHeight
}
});
}
// WS.onerror = console.error;
WS.onclose = (closeEvent) => {
if (closeEvent.code > 1001 && RETRY_COUNT < 10) {
RETRY_COUNT++;
console.warn("sitestat connection closed unexpectedly with code "+closeEvent.code+". Trying to reestablish the connection in " + RETRY_TIMEOUT + "ms.");
window.setTimeout(initWS, RETRY_TIMEOUT);
}
}
}
// The main script starts below:
if (window.navigator.webdriver) {
throw new Error("sitestat disabled for automated user agents.");
}
REMOTE_URL = getRemoteUrl();
initWS();
// Register various event listeners.
const events = ["click", "keyup", "keydown", "copy"];
for (let i = 0; i < events.length; i++) {
document.addEventListener(events[i], handleEvent);
}

View File

@ -11,6 +11,7 @@
<script src="scripts/themes.min.js"></script> <script src="scripts/themes.min.js"></script>
<noscript><style>.jsonly{display: none !important;}</style></noscript> <noscript><style>.jsonly{display: none !important;}</style></noscript>
<link rel="stylesheet" href="styles/style.css" type="text/css"> <link rel="stylesheet" href="styles/style.css" type="text/css">
<script src="scripts/sitestat.js?remote-url=sitestat.andrewlalis.com" async></script>
</head> </head>
<body> <body>