diff --git a/src/pat/inject/inject.js b/src/pat/inject/inject.js index 5081397a4..3b955a88a 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. @@ -818,14 +819,28 @@ 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; }, - _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 +978,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(/\