Skip to content

Commit

Permalink
rewrite: add document.querySelector() override, rewrite 'src^=https?'…
Browse files Browse the repository at this point in the history
…, '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)
  • Loading branch information
ikreymer authored Jun 17, 2024
1 parent 0e109a4 commit 27d341d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/wombat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -6804,6 +6838,8 @@ Wombat.prototype.wombatInit = function() {

this.initTimeoutIntervalOverrides();

this.initQuerySelectorOverride();

this.overrideSWAccess(this.$wbwindow);

// setAttribute
Expand Down

0 comments on commit 27d341d

Please sign in to comment.