philomena/assets/js/fp.js

52 lines
1 KiB
JavaScript
Raw Normal View History

2019-10-05 02:09:52 +02:00
/**
2024-05-07 19:33:56 +02:00
* Thanks uBlock for breaking our JS!
2019-10-05 02:09:52 +02:00
*/
// http://stackoverflow.com/a/34842797
function hashCode(str) {
return str.split('').reduce((prevHash, currVal) =>
((prevHash << 5) - prevHash) + currVal.charCodeAt(0), 0) >>> 0;
}
2024-05-07 19:33:56 +02:00
function createFp() {
2019-10-05 02:09:52 +02:00
const prints = [
navigator.userAgent,
navigator.cpuClass,
navigator.oscpu,
navigator.platform,
navigator.browserLanguage,
navigator.language,
navigator.systemLanguage,
navigator.userLanguage,
screen.availLeft,
screen.availTop,
screen.availWidth,
screen.height,
screen.width,
window.devicePixelRatio,
new Date().getTimezoneOffset(),
];
return hashCode(prints.join(''));
}
2024-05-07 19:33:56 +02:00
function setFpCookie() {
let fp;
2019-10-05 02:09:52 +02:00
// The prepended 'c' acts as a crude versioning mechanism.
try {
2024-05-07 19:33:56 +02:00
fp = `c${createFp()}`;
2019-10-05 02:09:52 +02:00
}
2024-05-07 19:33:56 +02:00
// If it fails, use fakeprint "c1836832948" as a last resort.
2019-10-05 02:09:52 +02:00
catch (err) {
2024-05-07 19:33:56 +02:00
fp = 'c1836832948';
2019-10-05 02:09:52 +02:00
}
2024-05-07 19:33:56 +02:00
document.cookie = `_ses=${fp}; path=/; SameSite=Lax`;
2019-10-05 02:09:52 +02:00
}
2024-05-07 19:33:56 +02:00
export { setFpCookie };