From 1d5c231424d84574ef450a3406916016765cfab9 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Sun, 31 Aug 2025 19:48:36 +0200 Subject: [PATCH 1/2] feat(pat-inject): Add new parameter "remove-tags". It takes a comma separated list of tag names which would be replaced in the inject response. This way you can allow to inject script tags. --- src/pat/inject/inject.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/pat/inject/inject.js b/src/pat/inject/inject.js index 5081397a4..0c5d6ed2d 100644 --- a/src/pat/inject/inject.js +++ b/src/pat/inject/inject.js @@ -38,6 +38,7 @@ parser.addArgument("class"); // Add a class to the injected content. parser.addArgument("history", "none", ["none", "record"]); parser.addArgument("push-marker"); parser.addArgument("scroll"); +parser.addArgument("remove-tags", "script", [], true); // Note: this should not be here but the parser would bail on unknown // parameters and expand/collapsible need to pass the url to us. @@ -824,8 +825,8 @@ const inject = { return true; }, - _sourcesFromHtml(html, url, sources) { - const $html = this._parseRawHtml(html, url); + _sourcesFromHtml(html, url, sources, cfg) { + const $html = this._parseRawHtml(html, url, cfg); return sources.map((source) => { if (source === "body") { source = "#__original_body"; @@ -963,17 +964,21 @@ const inject = { return page.innerHTML.trim(); }, - _parseRawHtml(html, url = "") { + _parseRawHtml(html, url = "", cfg = {}) { // remove script tags and head and replace body by a div const title = html.match(/\(.*)\<\/title\>/); let clean_html = html - .replace(/)<[^<]*)*<\/script>/gi, "") .replace(/)<[^<]*)*<\/head>/gi, "") .replace(/]*?)>/gi, "") .replace(/<\/html([^>]*?)>/gi, "") .replace(/]*?)>/gi, '
') .replace(/<\/body([^>]*?)>/gi, "
"); + for (const tag of cfg.removeTags || []) { + const re = RegExp(String.raw`<${tag}\b[^<]*(?:(?!<\/${tag}>)<[^<]*)*<\/${tag}>`, "gi") + clean_html = clean_html.replace(re, ""); + } + if (title && title.length == 2) { clean_html = title[0] + clean_html; } @@ -1114,7 +1119,7 @@ const inject = { sources(cfgs, data) { const sources = cfgs.map((cfg) => cfg.source); sources.push("title"); - const result = this._sourcesFromHtml(data, cfgs[0].url, sources); + const result = this._sourcesFromHtml(data, cfgs[0].url, sources, cfgs[0]); return result; }, }, From ff541e821d1e0980185f0131853efe8b71f448b6 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Mon, 1 Sep 2025 02:04:22 +0200 Subject: [PATCH 2/2] fix(pat-inject): Re-insert all script tags from the source so that they are executed. Otherwise they're not run nor is the script src downloaded. --- src/pat/inject/inject.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/pat/inject/inject.js b/src/pat/inject/inject.js index 0c5d6ed2d..3b955a88a 100644 --- a/src/pat/inject/inject.js +++ b/src/pat/inject/inject.js @@ -819,9 +819,23 @@ const inject = { elementbefore: "before", }[cfg.action]; - // Inject the content HERE! target[method](...source_nodes); + if (! cfg.removeTags?.includes("script")) { + // Find and execute scripts + for (const node of source_nodes) { + const scripts = node.querySelectorAll?.("script") || []; + for (const script of scripts) { + const new_script = document.createElement("script"); + for (const attr of [...script.attributes]) { + new_script.setAttribute(attr.name, attr.value) + } + new_script.textContent = script.textContent; + script.replaceWith(new_script); + } + } + } + return true; },