Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
# Changelog

## 1.0.4 — 2026-11-27

Fixes for pages that froze, and for the quirk form on the options page.

- Fixed ([#5](https://github.com/QuantIntellect/adaptive-dark-mode/issues/5)): pages could stop responding. Four
ways the extension could end up answering its own changes, found on an Oracle careers page and in tests:
- An inline `!important` color was transformed again on every pass, and transforms don't settle (a border color
keeps changing). With nothing else to adjust on the element, each pass was a new style change that the extension
then answered, in microtasks, so the page never painted or took input again. One element with
`style="border-color: #f0f0f0 !important"` was enough. Now the original value is kept and only transformed once,
and it is put back when the mode turns off (unless the page changed it in the meantime).
- Text over a large background photo was checked again on every frame, because the photo is never classified
(photos are left alone) and the "classified, look again" step ran anyway. On the Oracle page that rewrote the
same attributes 60 times a second, and the site's UI library (Oracle JET) re-renders components when attributes
under them change. Text is now checked again only when the image actually got a verdict.
- Adjustments are written only when their value changes. A write of the same value still reaches the page's own
observers, and some re-render on any change.
- Attribute changes that leave the value as it was (a framework re-applying the same classes) are ignored.
- A safety net for loops not covered above. Within one task an element is handled a few times at most. After 100
observer callbacks in one task, the rest waits for the next task, and if that happens three tasks in a row the
work is dropped until things calm down. Elements that keep coming back to the layout pass on every frame are looked
at again a second later. When this happens, the popup's statistics line says "loops stopped".
- Fixed ([#4](https://github.com/QuantIntellect/adaptive-dark-mode/issues/4)): after one invalid selector in the
options page's quirk form, every selector was rejected as "Not a valid CSS selector" until the page was reloaded
(the message stayed set on the field and blocked the form). The message now clears as soon as the field is edited,
and it names the selector that's wrong. The placeholders no longer read like part of the value ("Invert, e.g.
.chart img, .logo").
- Tests: a fixture with the loops above (an inline `!important` border, text over a photo, a component that
re-applies its children's classes, and one that re-renders fresh children whenever an attribute under it changes),
checked for a page that still answers, no attribute rewritten while idle, a stable border and colors restored on
turning off. Three checks for the quirk form.

## 1.0.3 — 2026-11-20

Sites that have a dark theme of their own, switched off in their own settings.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ handles what rules can't: text contrast against the real backdrop, small dark co

All checks run against the project's own fixtures in `test/fixtures/`: a recipe-site page that exercises
every rule case, opt-out pages, an already-dark page, a late-rendering SPA, stacked drawing canvases, a consent
dialog, a page whose body arrives late, sites with a dark theme of their own (no offer) and pages of dark sections
(offer kept). Pages that must be left alone
dialog, a page whose body arrives late, sites with a dark theme of their own (no offer), pages of dark sections
(offer kept) and pages that answer the extension's changes (no feedback loop). Pages that must be left alone
are also compared pixel for pixel.

| Browser | Mode | Fixture checks |
|---|---|---|
| Chrome for Testing 153.0.8010.12 | headless | 92 / 92 (1.0.3) |
| Chrome for Testing 153.0.8010.12 | headless | 104 / 104 (1.0.4) |
| Google Chrome 157.0.8103.40 (macOS) | headed | 65 / 66 (1.0.0, before the six auto-theme checks). The miss is the pixel-identical "turned off" comparison. About 2,200 pixels around the two scaled product images differ, by 1 level out of 255 (15 in an earlier run). The separate "nothing left behind" check (0 attributes, 0 custom properties, 0 sheets) passed. The cause has not been isolated. |

Real sites, with the extension on (text elements sampled after scrolling). Google Chrome 157 and Chrome for Testing 153 gave the
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "Adaptive Dark Mode",
"short_name": "Adaptive Dark",
"version": "1.0.3",
"version": "1.0.4",
"description": "Presents light websites as dark while you browse in dark mode, keeping hue, contrast, logos and images right.",
"homepage_url": "https://github.com/QuantIntellect/adaptive-dark-mode",
"minimum_chrome_version": "121",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adaptive-dark-mode",
"version": "1.0.3",
"version": "1.0.4",
"private": true,
"description": "Light websites presented as dark while you browse in dark mode. A Chrome extension modelled on Safari 27.3 Adaptive Dark Mode.",
"license": "MIT",
Expand Down
27 changes: 20 additions & 7 deletions src/content/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
constructor(ctx) {
this.ctx = ctx; // { model, settings, stats, classifyRemote(url), fetchText(url) }
this.verdicts = new Map(); // url -> Promise<verdict>
this.seen = new WeakMap(); // element -> url handled
this.seen = new WeakMap(); // <img> -> url handled
this.bgJobs = new WeakMap(); // element -> { url, promise } background image looked at
this.meta = new WeakMap(); // element -> verdict (for text-over-image decisions)
}

Expand Down Expand Up @@ -59,14 +60,26 @@
}
}

/** Small icon element painted with a background image, or a big tiled texture. */
async processBackground(el, cs) {
if (!this.ctx.settings.images) return;
/**
* Small icon element painted with a background image, or a big tiled
* texture. Resolves once that image has been looked at; calls for the
* same element and image share the one look. Afterwards verdictFor(el)
* is set only if the image was classified (most backgrounds, photos and
* artwork, are left alone and never are).
*/
processBackground(el, cs) {
if (!this.ctx.settings.images) return Promise.resolve();
const m = /url\(\s*["']?([^"')]+)["']?\s*\)/.exec(cs.backgroundImage);
if (!m) return;
if (!m) return Promise.resolve();
const url = m[1];
if (this.seen.get(el) === url) return;
this.seen.set(el, url);
const job = this.bgJobs.get(el);
if (job && job.url === url) return job.promise;
const promise = this.lookAtBackground(el, cs, url).catch(() => {});
this.bgJobs.set(el, { url, promise });
return promise;
}

async lookAtBackground(el, cs, url) {
const rect = el.getBoundingClientRect();
const iconLike = rect.width <= 96 && rect.height <= 96 && el.childElementCount === 0 && !el.textContent.trim();
const large = rect.width * rect.height > 100000;
Expand Down
48 changes: 31 additions & 17 deletions src/content/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
class InlineEngine {
constructor(ctx) {
this.ctx = ctx; // { model, stats }
this.important = new WeakMap(); // el -> { prop: [value, priority] } originals we replaced
this.important = new WeakMap(); // el -> { prop: { original, written } } !important declarations we swapped
}

processTree(node) {
Expand All @@ -78,6 +78,7 @@
const want = new Map(); // custom prop -> value
const ctx = { base: document.baseURI };
let pending = false;
let swapped = false;

for (let i = 0; i < style.length; i++) {
const prop = style[i];
Expand All @@ -94,17 +95,24 @@
pending = true;
continue;
}
const t = model.transformValue(value, role, ctx);
if (t === value) continue;
if (style.getPropertyPriority(prop) === 'important') {
// Inline !important beats every author sheet, so we have to swap the
// declaration itself (restored on shutdown).
// declaration itself (restored on shutdown). Never transform our own
// output again: transforms aren't idempotent (a border color drifts
// on every pass), and each pass would be a style write our observer
// sees, which froze pages (#5).
const saved = this.important.get(el) || {};
if (!saved[prop]) saved[prop] = value;
this.important.set(el, saved);
if (saved[prop] && saved[prop].written === value) continue; // ours, untouched since
const t = model.transformValue(value, role, ctx);
if (t === value) continue;
style.setProperty(prop, t, 'important');
saved[prop] = { original: value, written: style.getPropertyValue(prop) };
this.important.set(el, saved);
swapped = true;
continue;
}
const t = model.transformValue(value, role, ctx);
if (t === value) continue;
const tok = TOKEN_OF[prop];
if (!tok) continue;
tokens.push(tok);
Expand Down Expand Up @@ -154,29 +162,35 @@
if (!want.has(p)) stale.push(p);
}
}
if (!tokens.length && !want.size && !stale.length && !el.hasAttribute('data-adm-i')) return;
if (!swapped && !tokens.length && !want.size && !stale.length && !el.hasAttribute('data-adm-i')) return;
// Write only what changed. A write of the same value is still reported
// to the page's observers, and some re-render on any change (#5).
for (const p of stale) style.removeProperty(p);
for (const [p, v] of want) if (style.getPropertyValue(p) !== v) style.setProperty(p, v);
if (tokens.length) el.setAttribute('data-adm-i', tokens.join(' '));
else el.removeAttribute('data-adm-i');
const list = tokens.join(' ');
if (list && el.getAttribute('data-adm-i') !== list) el.setAttribute('data-adm-i', list);
else if (!list && el.hasAttribute('data-adm-i')) el.removeAttribute('data-adm-i');
if (tokens.length) this.ctx.stats.inline = (this.ctx.stats.inline || 0) + 1;
D.mark(el);
}

restore(el) {
/** Put back the author's !important declarations we swapped (if the page hasn't replaced them since). */
unswap(el) {
const saved = this.important.get(el);
if (saved) {
for (const [prop, value] of Object.entries(saved)) el.style.setProperty(prop, value, 'important');
this.important.delete(el);
if (!saved) return;
for (const [prop, s] of Object.entries(saved)) {
if (el.style.getPropertyValue(prop) === s.written) el.style.setProperty(prop, s.original, 'important');
}
this.important.delete(el);
}

restore(el) {
this.unswap(el);
D.clean(el);
}

restoreAll() {
for (const el of D.touched) {
const saved = this.important.get(el);
if (saved) for (const [prop, value] of Object.entries(saved)) el.style.setProperty(prop, value, 'important');
}
for (const el of D.touched) this.unswap(el);
}
}

Expand Down
42 changes: 38 additions & 4 deletions src/content/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
this.observed = new WeakSet();
this.surfaceSeen = new WeakMap(); // surface -> pass id
this.pass = 0;
this.frame = 0; // flush count
this.runs = new WeakMap(); // element -> { frame, run }: consecutive frames it was processed on
this.parked = new Set(); // elements held back from a re-queue loop, looked at again later
this.parkTimer = 0;
this.running = true;
this.visible = new Set();
this.io = new IntersectionObserver((entries) => {
Expand Down Expand Up @@ -82,6 +86,8 @@
stop() {
this.running = false;
this.io.disconnect();
clearTimeout(this.parkTimer);
this.parked.clear();
this.queue.clear();
this.visible.clear();
this.offscreen.clear();
Expand Down Expand Up @@ -143,9 +149,21 @@
flush() {
this.scheduled = false;
if (!this.running) return;
const frame = ++this.frame;
const start = performance.now();
for (const el of this.queue) {
this.queue.delete(el);
// Back in the queue on every frame for half a second: something
// (possibly its own processing) keeps re-queueing it. Park it and
// look again a second later, after the churn.
const r = this.runs.get(el);
const run = r && r.frame === frame - 1 ? r.run + 1 : 1;
this.runs.set(el, { frame, run });
if (run > 30) {
if (run === 31) this.ctx.stats.loopGuard = (this.ctx.stats.loopGuard || 0) + 1;
this.park(el);
continue;
}
try {
this.process(el);
} catch (_) {
Expand All @@ -156,6 +174,18 @@
if (this.queue.size) this.schedule();
}

park(el) {
this.parked.add(el);
if (this.parkTimer) return;
this.parkTimer = setTimeout(() => {
this.parkTimer = 0;
if (!this.running) return;
for (const p of this.parked) if (p.isConnected) this.queue.add(p);
this.parked.clear();
this.schedule();
}, 1000);
}

// -------------------------------------------------------------------------
process(el) {
if (!el.isConnected || el.closest('[data-adm-x]')) return;
Expand Down Expand Up @@ -217,9 +247,11 @@
const v = this.ctx.images.verdictFor(node);
if (!v) {
// Unclassified background image: classify it (it may be a light
// texture we remove), then look at this text again.
// texture we remove), then look at this text again, but only if
// that gave a verdict. Photos and artwork are never classified,
// and re-queueing on those re-checked the text on every frame (#5).
this.ctx.images.processBackground(node, cs).then(() => {
if (el.isConnected) {
if (el.isConnected && this.ctx.images.verdictFor(node)) {
this.queue.add(el);
this.schedule();
}
Expand Down Expand Up @@ -285,7 +317,9 @@
setFg(el, base, fixed) {
if (fixed) {
if (!el.hasAttribute('data-adm-fg')) this.ctx.stats.text = (this.ctx.stats.text || 0) + 1;
el.dataset.admBase = C.format(base);
// Same value, no write: the page's observers hear about every write.
const b = C.format(base);
if (el.dataset.admBase !== b) el.dataset.admBase = b;
D.setAdjust(el, 'data-adm-fg', '--adm-fg', C.format(fixed));
} else if (el.hasAttribute('data-adm-fg')) {
delete el.dataset.admBase;
Expand Down Expand Up @@ -407,7 +441,7 @@
if (!model.isVivid(p.c) && (invertAll || C.contrast(p.c, back) < 3)) next = model.flip(p.c);
const key = p.prop === 'fill' ? 'admFill' : 'admStroke';
if (next) {
p.sh.dataset[key] = C.format(p.c);
if (p.sh.dataset[key] !== C.format(p.c)) p.sh.dataset[key] = C.format(p.c);
D.setAdjust(p.sh, p.attr, p.cprop, C.format(next));
changed++;
} else if (p.sh.hasAttribute(p.attr)) {
Expand Down
Loading
Loading