From 07db46a90971adf0aa40647d0182c0caad25947e Mon Sep 17 00:00:00 2001 From: hackademix Date: Sat, 24 Aug 2024 09:38:02 +0200 Subject: [PATCH] Configurable "lazy_load" capability (see https://github.com/whatwg/html/issues/5250). --- src/_locales/en/messages.json | 3 +++ src/bg/LifeCycle.js | 5 +++++ src/content/content.js | 37 +++++++++++++++++++++++++++++++++ src/content/onScriptDisabled.js | 37 --------------------------------- src/nscl | 2 +- 5 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index 1658691..ef3f27d 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -572,6 +572,9 @@ "cap_noscript": { "message": "noscript" }, + "cap_lazy_load": { + "message": "lazy load" + }, "cap_unchecked_css": { "message": "unrestricted CSS" }, diff --git a/src/bg/LifeCycle.js b/src/bg/LifeCycle.js index 32c2d6d..54f5dab 100644 --- a/src/bg/LifeCycle.js +++ b/src/bg/LifeCycle.js @@ -324,6 +324,11 @@ var LifeCycle = (() => { ns.openOptionsPage({tab: 2, focus: "#opt-vintageTheme", hilite: "#sect-themes"}); })(); } + + if (Ver.is(previousVersion, "<=", "11.4.35rc2")) { + // add the lazy_load capability to any preset which already has the script capability + await configureNewCap("lazy_load", ["DEFAULT", "TRUSTED", "CUSTOM"], caps => caps.has("script")); + } }, async onUpdateAvailable(details) { diff --git a/src/content/content.js b/src/content/content.js index 647dc81..560fc03 100644 --- a/src/content/content.js +++ b/src/content/content.js @@ -183,6 +183,43 @@ ns.on("capabilities", () => { allowed: ns.canScript }); + if (!ns.allows("lazy_load")) { + // Force loading attributes to "eager", since CSP-based script blocking + // does not disable lazy loading as it should to address the privacy + // concerns mentioned in the specification. + // See https://gitlab.torproject.org/tpo/applications/tor-browser/-/issues/42805 + + const toEager = el => el.setAttribute("loading", "eager"); + const toEagerAll = parent => [... parent.querySelectorAll("[loading=lazy]")].forEach(toEager); + + toEagerAll(document); + if (document.readyState === "loading") { + + const observer = new MutationObserver(records => { + for (const r of records) { + console.log(r); + switch(r.type) { + case "attributes": + if (r.attributeName === "loading") { + toEager(r.target); + } + break; + case "subtree": + toEagerAll(r.target); + break; + } + + } + }); + + observer.observe(document.documentElement, {subtree: true, attributeFilter: ["loading"]}); + addEventListener("DOMContentLoaded", e => { + toEagerAll(document); + observer.disconnect(); + }); + } + } + if (!ns.allows("unchecked_css")) { // protection against CSS PP0 (https://orenlab.sise.bgu.ac.il/p/PP0) diff --git a/src/content/onScriptDisabled.js b/src/content/onScriptDisabled.js index 5e10dfe..d9d4759 100644 --- a/src/content/onScriptDisabled.js +++ b/src/content/onScriptDisabled.js @@ -46,43 +46,6 @@ function onScriptDisabled() { } } - { - // Force loading attributes to "eager", since CSP-based script blocking - // does not disable lazy loading as it should to address the privacy - // concerns mentioned in the specification. - // See https://gitlab.torproject.org/tpo/applications/tor-browser/-/issues/42805 - - const toEager = el => el.setAttribute("loading", "eager"); - const toEagerAll = parent => [... parent.querySelectorAll("[loading=lazy]")].forEach(toEager); - - toEagerAll(document); - if (document.readyState === "loading") { - - const observer = new MutationObserver(records => { - for (const r of records) { - console.log(r); - switch(r.type) { - case "attributes": - if (r.attributeName === "loading") { - toEager(r.target); - } - break; - case "subtree": - toEagerAll(r.target); - break; - } - - } - }); - - observer.observe(document.documentElement, {subtree: true, attributeFilter: ["loading"]}); - addEventListener("DOMContentLoaded", e => { - toEagerAll(document); - observer.disconnect(); - }); - } - } - let eraser = { tapped: null, delKey: false, diff --git a/src/nscl b/src/nscl index c599376..9cef1fb 160000 --- a/src/nscl +++ b/src/nscl @@ -1 +1 @@ -Subproject commit c59937693d3fd772add35e918eb90744ff55d148 +Subproject commit 9cef1fb442641dbf8e44ad70deb990828d5b5de3