From 12747394c0dc470627db39e8894cce0b2d8328b9 Mon Sep 17 00:00:00 2001 From: Martok Date: Tue, 16 Jan 2024 19:49:23 +0100 Subject: [PATCH] fix: place injected scripts after the tag so it remains in the first 1k bytes --- lib/main.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/main.js b/lib/main.js index 49f49ba..d1df954 100644 --- a/lib/main.js +++ b/lib/main.js @@ -820,6 +820,27 @@ class PolyfillService { return newHeader; } + getScriptInsertPoint(data, lookForMeta = false) { + const pHead = data.indexOf("", pCharset+16); + if (pPoint < 0) + return false; + return pPoint + 1; + } + } + // just insert directly after head + const pPoint = data.indexOf(">", pHead+5); + if (pPoint < 0) + return false; + return pPoint + 1; + } + modifyRequestData(data, activeFixes) { activeFixes.ensureCompiled(); if (activeFixes.contentReplace) { @@ -828,12 +849,10 @@ class PolyfillService { } } if (activeFixes.scripts) { - const p1 = data.indexOf("=0) { - const p2 = data.indexOf(">", p1+5); - if (p2 >= 0) { - data = data.slice(0, p2 + 1) + activeFixes.scripts + data.slice(p2 + 1); - } + const splicePos = this.getScriptInsertPoint(data.slice(0, 1024), true) || + this.getScriptInsertPoint(data); + if (splicePos >= 0) { + data = data.slice(0, splicePos) + activeFixes.scripts + data.slice(splicePos); } } return data;