Skip to content

Commit e56b6bd

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 3d05847 commit e56b6bd

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.
@@ -823,8 +824,8 @@ const inject = {
823824
return true;
824825
},
825826

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

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

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

0 commit comments

Comments
 (0)