Commit eabd9836 by 周田

fix: 避免CSP风险

parent 1de1afb8
...@@ -7,6 +7,7 @@ const htmlText = fs.readFileSync(htmlPath, 'utf8'); ...@@ -7,6 +7,7 @@ const htmlText = fs.readFileSync(htmlPath, 'utf8');
const htmlArr = htmlText.match(/.*\n/g) || []; const htmlArr = htmlText.match(/.*\n/g) || [];
let result = ""; let result = "";
let count = 1;
htmlArr.forEach(v => { htmlArr.forEach(v => {
v = v v = v
...@@ -18,6 +19,37 @@ htmlArr.forEach(v => { ...@@ -18,6 +19,37 @@ htmlArr.forEach(v => {
} }
}); });
let result_arr = result.match(/.*\n/g) || [];
result = ""
result_arr.forEach(v => {
// 处理内联脚本
if (v.includes(`script`)) {
let start_index = v.indexOf('>');
let end_index = v.lastIndexOf('<')
let script_str = v.substring(start_index + 1, end_index)
if (count === 1) {
let path = "./dist/assets/" + String(count) + ".js";
fs.writeFileSync(path, script_str, 'utf8');
path = "." + path.substring(6)
v = v.substring(0, start_index) + " src=\"" + path + "\">" + v.substring(end_index);
}
else if (count === 3) {
let path = "./dist/assets/" + String(count) + ".js";
v = v.substring(0, start_index + 1) + v.substring(end_index);
fs.writeFileSync(path, script_str, 'utf8');
path = "." + path.substring(6)
v += "\n <script src=\"" + path + "\"></script>";
}
count++;
// console.log(v);
}
if (!v.includes(`script type="module"`)) {
result += v;
}
})
fs.writeFileSync(htmlPath, result, 'utf8'); fs.writeFileSync(htmlPath, result, 'utf8');
console.log("处理完成"); console.log("处理完成");
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment