fix(bundle): mirror source fixes in embedded userscript
Mirrors the three src/ changes into the embedded copy in
torn-attribute-tracker.user.js:
1. Hoist the four let bindings (lastSnapshot, lastAttr, lastDelta,
anchorError) to the top of start(), before dialog.mount() and
applyMode(), so the anchor-miss branch of applyMode() can write
anchorError without tripping the TDZ.
2. Default the floating dialog to bottom-left (left: 20px, bottom: 20px)
in both Dialog.mount() and Dialog.setMode()'s 'free' branch.
3. One-time migration: if prefs.pos has any non-zero coordinate on load
(a residue of the bottom-right era), reset it to {x: 0, y: 0} and log
to the console. Stored position from any subsequent drag is preserved.
4. Wrap the start() body in try/catch and log failures to the console,
so an unexpected error (e.g. TornTools conflict, future regressions)
does not prevent the dialog from appearing.
All four changes are byte-equivalent to the corresponding src/ changes;
the build-time drift tests in tests/build.test.js still pass.
This commit is contained in:
@@ -315,7 +315,7 @@
|
|||||||
this.mode = opts.initialMode || 'free';
|
this.mode = opts.initialMode || 'free';
|
||||||
if (this.mode === 'free') {
|
if (this.mode === 'free') {
|
||||||
root.style.bottom = '20px';
|
root.style.bottom = '20px';
|
||||||
root.style.right = '20px';
|
root.style.left = '20px';
|
||||||
if (opts.initialPos && (opts.initialPos.x || opts.initialPos.y)) {
|
if (opts.initialPos && (opts.initialPos.x || opts.initialPos.y)) {
|
||||||
root.style.transform = 'translate(' + opts.initialPos.x + 'px, ' + opts.initialPos.y + 'px)';
|
root.style.transform = 'translate(' + opts.initialPos.x + 'px, ' + opts.initialPos.y + 'px)';
|
||||||
}
|
}
|
||||||
@@ -327,7 +327,7 @@
|
|||||||
this.mode = mode;
|
this.mode = mode;
|
||||||
if (!this.root) return;
|
if (!this.root) return;
|
||||||
this.root.style.transform = ''; this.root.style.top = ''; this.root.style.bottom = ''; this.root.style.left = ''; this.root.style.right = '';
|
this.root.style.transform = ''; this.root.style.top = ''; this.root.style.bottom = ''; this.root.style.left = ''; this.root.style.right = '';
|
||||||
if (mode === 'free') { this.root.style.bottom = '20px'; this.root.style.right = '20px'; }
|
if (mode === 'free') { this.root.style.bottom = '20px'; this.root.style.left = '20px'; }
|
||||||
else if (anchorInfo && anchorInfo.canAnchor) { this._positionAnchored(anchorInfo.rect); }
|
else if (anchorInfo && anchorInfo.canAnchor) { this._positionAnchored(anchorInfo.rect); }
|
||||||
else { this.root.style.top = '20px'; this.root.style.left = '50%'; this.root.style.transform = 'translateX(-50%)'; }
|
else { this.root.style.top = '20px'; this.root.style.left = '50%'; this.root.style.transform = 'translateX(-50%)'; }
|
||||||
}
|
}
|
||||||
@@ -446,87 +446,102 @@
|
|||||||
|
|
||||||
function start() {
|
function start() {
|
||||||
if (window.__tat_started) return; window.__tat_started = true;
|
if (window.__tat_started) return; window.__tat_started = true;
|
||||||
const store = new Store({ storage: localStorage, onWarn: function (m) { console.warn(m); } });
|
try {
|
||||||
const prefs = store.getPrefs();
|
const store = new Store({ storage: localStorage, onWarn: function (m) { console.warn(m); } });
|
||||||
|
const prefs = store.getPrefs();
|
||||||
|
|
||||||
const dialog = new Dialog({
|
// State that applyMode() and render() may touch on first call.
|
||||||
onTargetChange: function (v) {
|
// Declared up-front to avoid TDZ ReferenceError if applyMode()'s
|
||||||
const a = currentAttribute(); if (!a) return; store.setTarget(a.attr, v); render();
|
// anchor-miss branch fires before the natural declaration point.
|
||||||
},
|
let lastSnapshot = null;
|
||||||
onModeChange: function (m) { store.setMode(m); prefs.mode = m; applyMode(); },
|
let lastAttr = null;
|
||||||
onPosChange: function (pos) { store.setPos(pos); },
|
let lastDelta = 0;
|
||||||
onClose: function () { dialog.destroy(); },
|
let anchorError = null;
|
||||||
});
|
|
||||||
|
|
||||||
dialog.mount({ initialMode: prefs.mode, initialPos: prefs.pos });
|
// One-time migration: dialog now defaults to bottom-left, so reset any
|
||||||
applyMode();
|
// previously-saved position from the bottom-right era.
|
||||||
|
if (prefs.pos && (prefs.pos.x !== 0 || prefs.pos.y !== 0)) {
|
||||||
|
console.info('[tat] resetting dialog position to new bottom-left default');
|
||||||
|
prefs.pos = { x: 0, y: 0 };
|
||||||
|
store.setPos(prefs.pos);
|
||||||
|
}
|
||||||
|
|
||||||
let lastSnapshot = null;
|
const dialog = new Dialog({
|
||||||
let lastAttr = null;
|
onTargetChange: function (v) {
|
||||||
let lastDelta = 0;
|
const a = currentAttribute(); if (!a) return; store.setTarget(a.attr, v); render();
|
||||||
let anchorError = null;
|
},
|
||||||
|
onModeChange: function (m) { store.setMode(m); prefs.mode = m; applyMode(); },
|
||||||
|
onPosChange: function (pos) { store.setPos(pos); },
|
||||||
|
onClose: function () { dialog.destroy(); },
|
||||||
|
});
|
||||||
|
|
||||||
function snapshot() {
|
dialog.mount({ initialMode: prefs.mode, initialPos: prefs.pos });
|
||||||
const a = currentAttribute();
|
applyMode();
|
||||||
if (!a) return { error: "Couldn't read attribute — Torn may have updated the page." };
|
|
||||||
lastAttr = a.attr;
|
|
||||||
const summary = store.getSummary(a.attr);
|
|
||||||
return {
|
|
||||||
attr: a.attr, gym: a.gym, current: a.current,
|
|
||||||
target: store.getTarget(a.attr), perTrain: lastDelta, summary: summary,
|
|
||||||
warn: store._saveDisabled ? 'saving disabled this session' : null,
|
|
||||||
anchorError: anchorError,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function render() { lastSnapshot = snapshot(); dialog.render(lastSnapshot); }
|
function snapshot() {
|
||||||
|
const a = currentAttribute();
|
||||||
|
if (!a) return { error: "Couldn't read attribute — Torn may have updated the page." };
|
||||||
|
lastAttr = a.attr;
|
||||||
|
const summary = store.getSummary(a.attr);
|
||||||
|
return {
|
||||||
|
attr: a.attr, gym: a.gym, current: a.current,
|
||||||
|
target: store.getTarget(a.attr), perTrain: lastDelta, summary: summary,
|
||||||
|
warn: store._saveDisabled ? 'saving disabled this session' : null,
|
||||||
|
anchorError: anchorError,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function applyMode() {
|
function render() { lastSnapshot = snapshot(); dialog.render(lastSnapshot); }
|
||||||
if (prefs.mode === 'anchored') {
|
|
||||||
const el = findAnchorElement();
|
function applyMode() {
|
||||||
if (el) {
|
if (prefs.mode === 'anchored') {
|
||||||
const rect = el.getBoundingClientRect();
|
const el = findAnchorElement();
|
||||||
dialog.setMode('anchored', { canAnchor: true });
|
if (el) {
|
||||||
dialog._positionAnchored(rect);
|
const rect = el.getBoundingClientRect();
|
||||||
if (typeof ResizeObserver !== 'undefined') {
|
dialog.setMode('anchored', { canAnchor: true });
|
||||||
const ro = new ResizeObserver(function () {
|
dialog._positionAnchored(rect);
|
||||||
if (prefs.mode === 'anchored') dialog._positionAnchored(el.getBoundingClientRect());
|
if (typeof ResizeObserver !== 'undefined') {
|
||||||
});
|
const ro = new ResizeObserver(function () {
|
||||||
ro.observe(el);
|
if (prefs.mode === 'anchored') dialog._positionAnchored(el.getBoundingClientRect());
|
||||||
|
});
|
||||||
|
ro.observe(el);
|
||||||
|
}
|
||||||
|
anchorError = null;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
anchorError = null;
|
// Anchor selector missed — don't snap to default, just keep current
|
||||||
|
// position and show a note.
|
||||||
|
anchorError = "Couldn't find the training form on this page.";
|
||||||
|
render();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Anchor selector missed — don't snap to default, just keep current
|
anchorError = null;
|
||||||
// position and show a note.
|
dialog.setMode('free');
|
||||||
anchorError = "Couldn't find the training form on this page.";
|
|
||||||
render();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
anchorError = null;
|
|
||||||
dialog.setMode('free');
|
|
||||||
}
|
|
||||||
|
|
||||||
render();
|
render();
|
||||||
|
|
||||||
let pending = false;
|
let pending = false;
|
||||||
const observer = new MutationObserver(function () {
|
const observer = new MutationObserver(function () {
|
||||||
if (pending) return;
|
if (pending) return;
|
||||||
pending = true;
|
pending = true;
|
||||||
requestAnimationFrame(function () {
|
requestAnimationFrame(function () {
|
||||||
pending = false;
|
pending = false;
|
||||||
const a = currentAttribute();
|
const a = currentAttribute();
|
||||||
if (a && (a.attr !== lastAttr || a.current !== (lastSnapshot && lastSnapshot.current))) render();
|
if (a && (a.attr !== lastAttr || a.current !== (lastSnapshot && lastSnapshot.current))) render();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
observer.observe(document.body, { childList: true, subtree: true, characterData: true });
|
||||||
observer.observe(document.body, { childList: true, subtree: true, characterData: true });
|
|
||||||
|
|
||||||
const prev = (currentAttribute() && currentAttribute().current) || 0;
|
const prev = (currentAttribute() && currentAttribute().current) || 0;
|
||||||
startRequestInterceptor({
|
startRequestInterceptor({
|
||||||
prevValue: prev, currentAttr: lastAttr,
|
prevValue: prev, currentAttr: lastAttr,
|
||||||
onTrain: function (e) { store.recordTrain(e.attr, e.delta, e.ts); lastDelta = e.delta; render(); },
|
onTrain: function (e) { store.recordTrain(e.attr, e.delta, e.ts); lastDelta = e.delta; render(); },
|
||||||
onParseFail: function (url) { console.warn('[tat] could not parse train response from', url); },
|
onParseFail: function (url) { console.warn('[tat] could not parse train response from', url); },
|
||||||
});
|
});
|
||||||
|
} catch (e) {
|
||||||
|
console.error('[tat] failed to start:', e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== self-test (only when location.hash === '#tat-test') =====
|
// ===== self-test (only when location.hash === '#tat-test') =====
|
||||||
|
|||||||
Reference in New Issue
Block a user