Skip to content

Commit c154bb4

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 3b0cdbc commit c154bb4

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

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.
@@ -832,8 +833,8 @@ const inject = {
832833
return true;
833834
},
834835

835-
_sourcesFromHtml(html, url, sources) {
836-
const $html = this._parseRawHtml(html, url);
836+
_sourcesFromHtml(html, url, sources, cfg) {
837+
const $html = this._parseRawHtml(html, url, cfg);
837838
return sources.map((source) => {
838839
if (source === "body") {
839840
source = "#__original_body";
@@ -971,17 +972,21 @@ const inject = {
971972
return page.innerHTML.trim();
972973
},
973974

974-
_parseRawHtml(html, url = "") {
975+
_parseRawHtml(html, url = "", cfg = {}) {
975976
// remove script tags and head and replace body by a div
976977
const title = html.match(/\<title\>(.*)\<\/title\>/);
977978
let clean_html = html
978-
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "")
979979
.replace(/<head\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/head>/gi, "")
980980
.replace(/<html([^>]*?)>/gi, "")
981981
.replace(/<\/html([^>]*?)>/gi, "")
982982
.replace(/<body([^>]*?)>/gi, '<div id="__original_body">')
983983
.replace(/<\/body([^>]*?)>/gi, "</div>");
984984

985+
for (const tag of cfg.removeTags || []) {
986+
const re = RegExp(String.raw`<${tag}\b[^<]*(?:(?!<\/${tag}>)<[^<]*)*<\/${tag}>`, "gi")
987+
clean_html = clean_html.replace(re, "");
988+
}
989+
985990
if (title && title.length == 2) {
986991
clean_html = title[0] + clean_html;
987992
}
@@ -1122,7 +1127,7 @@ const inject = {
11221127
sources(cfgs, data) {
11231128
const sources = cfgs.map((cfg) => cfg.source);
11241129
sources.push("title");
1125-
const result = this._sourcesFromHtml(data, cfgs[0].url, sources);
1130+
const result = this._sourcesFromHtml(data, cfgs[0].url, sources, cfgs[0]);
11261131
return result;
11271132
},
11281133
},

0 commit comments

Comments
 (0)