port staffhider to ts, eslint --fix, new fp

This commit is contained in:
Luna D. 2024-06-04 21:13:31 +02:00
parent 0877ac685b
commit 97577db065
No known key found for this signature in database
GPG key ID: 4B1C63448394F688
4 changed files with 92 additions and 29 deletions

View file

@ -1,48 +1,109 @@
/** /**
* Thanks uBlock for breaking our JS! * FP version 4
*
* Not reliant on deprecated properties,
* and potentially more accurate at what it's supposed to do.
*/ */
import { $ } from './utils/dom';
import store from './utils/store';
// http://stackoverflow.com/a/34842797 // http://stackoverflow.com/a/34842797
function hashCode(str) { function hashCode(str) {
return str.split('').reduce((prevHash, currVal) => return str.split('').reduce((prevHash, currVal) =>
((prevHash << 5) - prevHash) + currVal.charCodeAt(0), 0) >>> 0; ((prevHash << 5) - prevHash) + currVal.charCodeAt(0), 0) >>> 0;
} }
function createFp() { async function createFp() {
let kb = 'none';
let mem = '1';
let ua = 'none';
if (navigator.keyboard) {
kb = (await navigator.keyboard.getLayoutMap()).entries().toArray().sort().map(e => `${e[0]}${e[1]}`).join('');
}
if (navigator.deviceMemory) {
mem = navigator.deviceMemory.toString();
}
if (navigator.userAgentData) {
const uadata = navigator.userAgentData;
let brands = 'none';
if (uadata.brands && uadata.brands.length > 0) {
brands = uadata.brands.filter(e => !e.brand.match(/.*ot.*rand.*/gi)).map(e => `${e.brand}${e.version}`).join('');
}
ua = `${brands}${uadata.mobile}${uadata.platform}`;
}
let width = store.get('cached_rem_size');
const body = $('body');
if (!width && body) {
const testElement = document.createElement('span');
testElement.style.minWidth = '1rem';
testElement.style.maxWidth = '1rem';
testElement.style.position = 'absolute';
body.appendChild(testElement);
width = testElement.clientWidth.toString();
body.removeChild(testElement);
store.set('cached_rem_size', width);
}
if (!width) {
width = '0';
}
const prints = [ const prints = [
navigator.userAgent, navigator.userAgent,
navigator.cpuClass, navigator.hardwareConcurrency.toString(),
navigator.oscpu, navigator.maxTouchPoints.toString(),
navigator.platform,
navigator.browserLanguage,
navigator.language, navigator.language,
navigator.systemLanguage, kb,
navigator.userLanguage, mem,
ua,
width,
screen.availLeft, screen.height.toString(),
screen.availTop, screen.width.toString(),
screen.availWidth, screen.colorDepth.toString(),
screen.height, screen.pixelDepth.toString(),
screen.width,
window.devicePixelRatio, window.devicePixelRatio.toString(),
new Date().getTimezoneOffset(), new Date().getTimezoneOffset().toString(),
]; ];
return hashCode(prints.join('')); return hashCode(prints.join(''));
} }
function setFpCookie() { async function setFpCookie() {
let fp; let fp = store.get('cached_ses_value');
// The prepended 'c' acts as a crude versioning mechanism. if (!fp) {
try { const m = document.cookie.match(/_ses=([a-f\d]+)/);
fp = `c${createFp()}`;
if (m && m[1]) {
fp = m[1];
}
} }
// If it fails, use fakeprint "c1836832948" as a last resort.
catch (err) { if (!fp || fp.charAt(0) !== 'd') {
fp = 'c1836832948'; // The prepended 'd' acts as a crude versioning mechanism.
try {
fp = `d${await createFp()}`;
}
// If it fails, use fakeprint "d1836832948" as a last resort.
catch (err) {
fp = 'd1836832948';
}
store.set('cached_ses_value', fp);
} }
document.cookie = `_ses=${fp}; path=/; SameSite=Lax`; document.cookie = `_ses=${fp}; path=/; SameSite=Lax`;

View file

@ -6,12 +6,10 @@
import { $$ } from './utils/dom'; import { $$ } from './utils/dom';
function hideStaffTools() { export function hideStaffTools() {
if (window.booru.hideStaffTools === 'true') { if (window.booru.hideStaffTools === 'true') {
$$('.js-staff-action').forEach(el => { $$<HTMLElement>('.js-staff-action').forEach(el => {
el.classList.add('hidden'); el.classList.add('hidden');
}); });
} }
} }
export { hideStaffTools };

View file

@ -6,7 +6,7 @@ describe('Linear interpolation', () => {
expect(lerp(0.5, 0, 100)).toEqual(50); expect(lerp(0.5, 0, 100)).toEqual(50);
expect(lerp(0.75, 0, 100)).toEqual(75); expect(lerp(0.75, 0, 100)).toEqual(75);
}); });
it('should clamp the value between min and max', () => { it('should clamp the value between min and max', () => {
expect(lerp(-999, 0, 100)).toEqual(0); expect(lerp(-999, 0, 100)).toEqual(0);
expect(lerp(0, 0, 100)).toEqual(0); expect(lerp(0, 0, 100)).toEqual(0);

View file

@ -65,6 +65,10 @@ interface BooruObject {
spoileredFilter: AstMatcher; spoileredFilter: AstMatcher;
tagsVersion: number; tagsVersion: number;
interactions: Interaction[]; interactions: Interaction[];
/**
* Indicates whether sensitive staff-only info should be hidden or not.
*/
hideStaffTools: string;
} }
declare global { declare global {