Skip to content

Commit 45fe069

Browse files
committed
feat(customScript): Support inject custom script into generated html head.
1 parent 8599584 commit 45fe069

File tree

7 files changed

+39
-3
lines changed

7 files changed

+39
-3
lines changed

src/baseTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export interface InvioPluginSettings {
114114

115115
customFooter?: string;
116116
customStyle?: string;
117+
customScript?: string;
117118

118119
/**
119120
* @deprecated

src/exporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const exportFile = async (
4949
const { s3BucketName: bucket, s3Endpoint: endpoint } = remoteClient.s3Config;
5050
remoteCOSDomain = `https://${bucket}.${endpoint}/`;
5151
}
52-
const customPageSettings: ICustomPageSettings = { footer: settings.customFooter, style: settings.customStyle };
52+
const customPageSettings: ICustomPageSettings = { script: settings.customScript, footer: settings.customFooter, style: settings.customStyle };
5353
await HTMLGenerator.generateWebpage(exportedFile, rootPath, view, remoteCOSDomain, customPageSettings);
5454
}
5555
catch (e)

src/html-generation/html-generator.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Utils } from "src/utils/utils";
1414
export interface ICustomPageSettings {
1515
footer?: string;
1616
style?: string;
17+
script?: string; // head 中的js脚本,支持外链
1718
}
1819
const LogoSVGDefault = `<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="svg238067" height="768px" width="1024px" version="1.1" viewBox="0 0 100 100" class="svg-icon invio-sync-wait"><g fill-rule="evenodd" style="transform: scale3d(0.89, 0.99, 1.5);"><path d="M27 97.93A56.08 56.08 0 0 1 9.29 19.08 55.77 55.77 0 0 0 23.59 50l.07.07c.53.58 1.06 1.14 1.62 1.7s1.12 1.09 1.72 1.62L45.54 72a14.93 14.93 0 0 1 4.53 10.93v1.59a15.12 15.12 0 0 1-8 13.52A15.09 15.09 0 0 1 27 97.93z" style="fill: var(--icon-color);"></path><path d="M23.59 50a55.77 55.77 0 0 1-14.3-30.92A56.46 56.46 0 0 1 27 2.08 15.08 15.08 0 0 1 42.11 2a15.12 15.12 0 0 1 8 13.52v1.59A15 15 0 0 1 45.55 28l-22 22z" fill="#999999" opacity=".8"></path><path d="M85.16 2.08a56.08 56.08 0 0 1 17.67 78.84A55.77 55.77 0 0 0 88.53 50l-.08-.07c-.52-.58-1.06-1.14-1.62-1.7s-1.12-1.09-1.69-1.62L66.58 28a14.93 14.93 0 0 1-4.53-10.93v-1.55A15.12 15.12 0 0 1 70 2a15.08 15.08 0 0 1 15.15.08z" style="fill: var(--icon-color);"></path><path d="M88.53 50a55.77 55.77 0 0 1 14.3 30.92 56.35 56.35 0 0 1-17.67 17 15.46 15.46 0 0 1-23.11-13.44v-1.59A15 15 0 0 1 66.57 72l22-22z" fill="#999999" opacity=".8"></path></g></svg>`
1920

@@ -185,7 +186,7 @@ export class HTMLGenerator {
185186

186187

187188
await this.appendFooter(file);
188-
await this.fillInHead(file, rootPath, remoteDomain);
189+
await this.fillInHead(file, rootPath, remoteDomain, customPageSettings);
189190

190191
file.downloads.unshift(file.getSelfDownloadable());
191192

@@ -443,7 +444,7 @@ export class HTMLGenerator {
443444
return { mediaPath: imagePath, jsPath: jsPath, cssPath: cssPath, rootPath: rootPath };
444445
}
445446

446-
private static async fillInHead(file: ExportFile, rootPath: Path, remoteDomain: string = '') {
447+
private static async fillInHead(file: ExportFile, rootPath: Path, remoteDomain: string = '', customPageSettings?: ICustomPageSettings) {
447448
const pageConfig = await this.getMeta(file);
448449
log.info('get file metadata: ', pageConfig, remoteDomain);
449450
log.info('head with remote domain: ', remoteDomain);
@@ -520,6 +521,16 @@ export class HTMLGenerator {
520521
scripts += `\n<script src='${remoteDomain}${remoteDomain}${relativePaths.jsPath}/webpage.js'></script>\n`;
521522
}
522523

524+
if (customPageSettings.script) {
525+
// 确保js外链资源合法
526+
if (customPageSettings.script?.startsWith('http') && customPageSettings.script?.endsWith('.js')) {
527+
scripts += `\n<script src='${customPageSettings.script}'></script>\n`;
528+
} else if (customPageSettings.script?.startsWith('<script>')) {
529+
scripts += `\n${customPageSettings.script}\n`
530+
} else {
531+
scripts += `\n<script>\n${customPageSettings.script}\n</script>\n`;
532+
}
533+
}
523534

524535
// --- CSS ---
525536
let cssSettings = document.getElementById("css-settings-manager")?.innerHTML ?? "";

src/langs/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@
177177
"settings_customstyle": "Custom Style",
178178
"settings_customstyle_desc": "Costom style css contents.",
179179

180+
"settings_customscript": "Custom Script",
181+
"settings_customscript_desc": "Costom script inject to html header.",
182+
180183
"settings_checkonnectivity": "Check Connectivity",
181184
"settings_checkonnectivity_desc": "Check connectivity.",
182185
"settings_checkonnectivity_button": "Check",

src/langs/zh_cn.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@
176176
"settings_customstyle": "自定义样式",
177177
"settings_customstyle_desc": "自定义Css样式",
178178

179+
"settings_customscript": "自定义Script",
180+
"settings_customscript_desc": "要插入html header中的script内容,支持script函数或者外链如https://docs.webinfra.cloud/demo.js",
181+
179182
"settings_checkonnectivity": "检查可否连接",
180183
"settings_checkonnectivity_desc": "检查可否连接。",
181184
"settings_checkonnectivity_button": "检查",

src/langs/zh_tw.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@
175175
"settings_customstyle": "自定义样式",
176176
"settings_customstyle_desc": "自定义Css样式",
177177

178+
"settings_customscript": "自定义Script",
179+
"settings_customscript_desc": "要插入html header中的script内容,支持script函数或者外链如https://docs.webinfra.cloud/demo.js",
180+
178181
"settings_checkonnectivity": "檢查可否連線",
179182
"settings_checkonnectivity_desc": "檢查可否連線。",
180183
"settings_checkonnectivity_button": "檢查",

src/settings.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const DEFAULT_SETTINGS: InvioPluginSettings = {
8282
skipSizeLargerThan: -1,
8383
customFooter: '',
8484
customStyle: '',
85+
customScript: '',
8586
};
8687

8788
export const getDEFAULT_SETTINGS = (): InvioPluginSettings => {
@@ -112,6 +113,7 @@ export const getDEFAULT_SETTINGS = (): InvioPluginSettings => {
112113
skipSizeLargerThan: -1,
113114
customFooter: '',
114115
customStyle: '',
116+
customScript: '',
115117
});
116118
}
117119

@@ -1051,6 +1053,19 @@ export class InvioSettingTab extends PluginSettingTab {
10511053
await this.plugin.saveSettings();
10521054
})
10531055
);
1056+
1057+
new Setting(basicDiv)
1058+
.setName(t("settings_customscript"))
1059+
.setDesc(t("settings_customscript_desc"))
1060+
.addTextArea((text) =>
1061+
text
1062+
.setPlaceholder("")
1063+
.setValue(`${this.plugin.settings.customScript}`)
1064+
.onChange(async (value) => {
1065+
this.plugin.settings.customScript = value.trim();
1066+
await this.plugin.saveSettings();
1067+
})
1068+
);
10541069

10551070
//////////////////////////////////////////////////
10561071
// below for advanced settings

0 commit comments

Comments
 (0)