Skip to content

Commit

Permalink
rewrite: follow up to #118, for document.write override, still perfor…
Browse files Browse the repository at this point in the history
…m sync write() when document is still loading to avoid synchronous code failing when expecting written HTML to be there, then perform service-worker based blob assign from #118 (fixes webrecorder/replayweb.page#332) (#147)
  • Loading branch information
ikreymer authored Jun 17, 2024
1 parent 7c6aaa4 commit f31c7be
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/wombat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5049,7 +5049,7 @@ Wombat.prototype.initDocWriteOpenCloseOverride = function() {
function docWrite(fnThis, originalFn, string) {
var win = wombat.$wbwindow;

if (isSWLoad() || (document.readyState === 'loading' && wombat.wb_info.injectDocClose)) {
if (isSWLoad() || (fnThis.readyState === 'loading' && wombat.wb_info.injectDocClose)) {
wombat._writeBuff += string;
return;
}
Expand Down Expand Up @@ -5107,10 +5107,26 @@ Wombat.prototype.initDocWriteOpenCloseOverride = function() {
var originalClose = $wbDocument.close;
var newClose = function close() {
if (wombat._writeBuff) {
// if loading, apply as may be sync waiting for changes
if (this.readyState === 'loading') {
orig_doc_write.call($wbDocument, wombat.rewriteHtml(wombat._writeBuff, true));
}
if (isSWLoad()) {
wombat.blobUrlForIframe(wombat.$wbwindow.frameElement, wombat._writeBuff);
} else if (document.readyState === 'loading') {
orig_doc_write.call($wbDocument, wombat.rewriteHtml(wombat._writeBuff, true));

const doc = this;

try {
// catch any sync changes to doc being replaced, and create updated blob
if (doc.documentElement) {
const m = new wombat.$wbwindow.MutationObserver((mut, obs) => {
wombat.blobUrlForIframe(wombat.$wbwindow.frameElement, doc.documentElement.outerHTML);
});
m.observe(doc.documentElement, { childList: true, subtree: true });
}
} catch (e) {
// ignore
}
}
wombat._writeBuff = '';
return;
Expand Down

0 comments on commit f31c7be

Please sign in to comment.