Skip to content

Commit 1d5c231

Browse files
committed
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.
1 parent 1e08ee5 commit 1d5c231

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/pat/inject/inject.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ parser.addArgument("class"); // Add a class to the injected content.
3838
parser.addArgument("history", "none", ["none", "record"]);
3939
parser.addArgument("push-marker");
4040
parser.addArgument("scroll");
41+
parser.addArgument("remove-tags", "script", [], true);
4142

4243
// Note: this should not be here but the parser would bail on unknown
4344
// parameters and expand/collapsible need to pass the url to us.
@@ -824,8 +825,8 @@ const inject = {
824825
return true;
825826
},
826827

827-
_sourcesFromHtml(html, url, sources) {
828-
const $html = this._parseRawHtml(html, url);
828+
_sourcesFromHtml(html, url, sources, cfg) {
829+
const $html = this._parseRawHtml(html, url, cfg);
829830
return sources.map((source) => {
830831
if (source === "body") {
831832
source = "#__original_body";
@@ -963,17 +964,21 @@ const inject = {
963964
return page.innerHTML.trim();
964965
},
965966

966-
_parseRawHtml(html, url = "") {
967+
_parseRawHtml(html, url = "", cfg = {}) {
967968
// remove script tags and head and replace body by a div
968969
const title = html.match(/\<title\>(.*)\<\/title\>/);
969970
let clean_html = html
970-
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "")
971971
.replace(/<head\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/head>/gi, "")
972972
.replace(/<html([^>]*?)>/gi, "")
973973
.replace(/<\/html([^>]*?)>/gi, "")
974974
.replace(/<body([^>]*?)>/gi, '<div id="__original_body">')
975975
.replace(/<\/body([^>]*?)>/gi, "</div>");
976976

977+
for (const tag of cfg.removeTags || []) {
978+
const re = RegExp(String.raw`<${tag}\b[^<]*(?:(?!<\/${tag}>)<[^<]*)*<\/${tag}>`, "gi")
979+
clean_html = clean_html.replace(re, "");
980+
}
981+
977982
if (title && title.length == 2) {
978983
clean_html = title[0] + clean_html;
979984
}
@@ -1114,7 +1119,7 @@ const inject = {
11141119
sources(cfgs, data) {
11151120
const sources = cfgs.map((cfg) => cfg.source);
11161121
sources.push("title");
1117-
const result = this._sourcesFromHtml(data, cfgs[0].url, sources);
1122+
const result = this._sourcesFromHtml(data, cfgs[0].url, sources, cfgs[0]);
11181123
return result;
11191124
},
11201125
},

0 commit comments

Comments
 (0)