Script simplification: Final minor cleanups

This commit is contained in:
Ali Khaleqi Yekta 2025-05-04 12:24:23 +03:30
parent 5369144b64
commit 9b9cf32ee7
No known key found for this signature in database
GPG key ID: 8CE90F8CC149DE8C

View file

@ -20,7 +20,7 @@
const attr = currentScript.getAttribute.bind(currentScript); const attr = currentScript.getAttribute.bind(currentScript);
const website = attr(_data + 'website-id'); const website = attr(_data + 'website-id');
const hostUrl = attr(_data + 'host-url'); const hostUrl = attr(_data + 'host-url');
const tag = attr(_data + 'tag'); const tag = attr(_data + 'tag') || undefined;
const autoTrack = attr(_data + 'auto-track') !== _false; const autoTrack = attr(_data + 'auto-track') !== _false;
const dnt = attr(_data + 'do-not-track') === _true; const dnt = attr(_data + 'do-not-track') === _true;
const excludeSearch = attr(_data + 'exclude-search') === _true; const excludeSearch = attr(_data + 'exclude-search') === _true;
@ -45,7 +45,7 @@
hostname, hostname,
url: currentUrl, url: currentUrl,
referrer: currentRef, referrer: currentRef,
tag: tag ? tag : undefined, tag,
}); });
const hasDoNotTrack = () => { const hasDoNotTrack = () => {
@ -73,10 +73,8 @@
const handlePathChanges = () => { const handlePathChanges = () => {
const hook = (_this, method, callback) => { const hook = (_this, method, callback) => {
const orig = _this[method]; const orig = _this[method];
return (...args) => { return (...args) => {
callback.apply(null, args); callback.apply(null, args);
return orig.apply(_this, args); return orig.apply(_this, args);
}; };
}; };
@ -87,14 +85,13 @@
const handleClicks = () => { const handleClicks = () => {
const trackElement = async el => { const trackElement = async el => {
const attr = el.getAttribute.bind(el); const eventName = el.getAttribute(eventNameAttribute);
const eventName = attr(eventNameAttribute);
if (eventName) { if (eventName) {
const eventData = {}; const eventData = {};
el.getAttributeNames().forEach(name => { el.getAttributeNames().forEach(name => {
const match = name.match(eventRegex); const match = name.match(eventRegex);
if (match) eventData[match[1]] = attr(name); if (match) eventData[match[1]] = el.getAttribute(name);
}); });
return track(eventName, eventData); return track(eventName, eventData);
@ -106,8 +103,7 @@
if (!parentElement) return trackElement(el); if (!parentElement) return trackElement(el);
const { href, target } = parentElement; const { href, target } = parentElement;
const eventName = parentElement.getAttribute(eventNameAttribute); if (!parentElement.getAttribute(eventNameAttribute)) return;
if (!eventName) return;
if (parentElement.tagName === 'BUTTON') { if (parentElement.tagName === 'BUTTON') {
return trackElement(parentElement); return trackElement(parentElement);
@ -158,7 +154,7 @@
cache = data.cache; cache = data.cache;
} }
} catch (e) { } catch (e) {
/* empty */ /* no-op */
} }
}; };