webrecorder / wombat

Wombat.js client-side rewriting library
GNU Affero General Public License v3.0
81 stars 31 forks source link

Doc Write Buffer on Load #124

Closed ikreymer closed 11 months ago

ikreymer commented 11 months ago

When overriding document.write() while page is loading (in inline scripts), buffer all write() calls within the script tag, and rewrite at the end. Allows for more accurate rewriting of partial HTML split over multiple write calls, eg.

<script>
document.write(`<div><a href="./some/link.html">`);
document.write(`Link</a>`);
document.write(`</div>`);
</script>

However, for this to work, a document.close() call must be injected into the script, so it becomes

<script>
document.write(`<div><a href="./some/link.html">`);
document.write(`Link</a>`);
document.write(`</div>`);
document.close();
</script>

The document.close() injection must happen server-side, so this is only enabled if wombat.injectDocWrite flag is set, otherwise per-call rewriting is used.

Also added improvements to try to detect partial tags that are removed if doing per-call rewriting

Fixes issue in #123