Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Code golf, makes getCookie function smaller

Uku Taht45bb8a637e16
diff --git a/assets/js/plausible.js b/assets/js/plausible.js
index 91fa35d766..e5fd4b8bbf 100644
--- a/assets/js/plausible.js
+++ b/assets/js/plausible.js
@@ -17,14 +17,10 @@
}
function getCookie(name) {
- var nameEQ = name + "=";
- var ca = document.cookie.split(';');
- for(var i=0;i < ca.length;i++) {
- var c = ca[i];
- while (c.charAt(0)==' ') c = c.substring(1,c.length);
- if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
- }
- return null;
+ let matches = document.cookie.match(new RegExp(
+ "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
+ ));
+ return matches ? decodeURIComponent(matches[1]) : null;
}
function pseudoUUIDv4() {