From 27d341da14687e948c00a784869ccafdd9576fb2 Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Mon, 17 Jun 2024 13:59:01 -0700 Subject: [PATCH] rewrite: add document.querySelector() override, rewrite 'src^=https?', 'href^=https?' query matches to instead be 'src*=' / 'href*=' to match rewritten URLs (fixes shorthand.com sites, eg: https://www.cambridge.org/news-and-insights/five-books-on-climate-action-for-cop28, webrecorder/replayweb.page#272) (#150) --- src/wombat.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/wombat.js b/src/wombat.js index 0631d0a..236e427 100755 --- a/src/wombat.js +++ b/src/wombat.js @@ -4859,6 +4859,40 @@ Wombat.prototype.initCreateElementNSFix = function() { this.$wbwindow.document.createElementNS = createElementNS; }; +/** + * Applies an override to document.querySelector() + * to override exact/prefix 'src' / 'href' queries + */ +Wombat.prototype.initQuerySelectorOverride = function() { + if ( + !this.$wbwindow.document.querySelector || + !this.$wbwindow.Document.prototype.querySelector + ) { + return; + } + var orig_QA = this.$wbwindow.document.querySelector; + var wombat = this; + + var querySelector = function(query) { + if (typeof(query) === 'string') { + try { + query = query.replace(/((?:^|\s)\b\w+\[(?:src|href))[\^]?(=['"]?(?:https?[:])?\/\/)/, '$1*$2'); + } catch (e) { + // ignore + } + } + return orig_QA.call( + wombat.proxyToObj(this), + query + ); + }; + + this.$wbwindow.Document.prototype.querySelector = querySelector; + this.$wbwindow.document.querySelector = querySelector; +}; + + + /** * Applies an override to Element.insertAdjacentHTML in order to ensure * that the strings of HTML to be inserted are rewritten and to @@ -6804,6 +6838,8 @@ Wombat.prototype.wombatInit = function() { this.initTimeoutIntervalOverrides(); + this.initQuerySelectorOverride(); + this.overrideSWAccess(this.$wbwindow); // setAttribute