perf: 优化自定义网页面板url生成机制, 使用blob url来生成临时地址

pull/70/head
moonrailgun 2 years ago
parent e4b5e9631e
commit c3d585650c

@ -4,7 +4,6 @@
"version": "0.0.0", "version": "0.0.0",
"private": true, "private": true,
"dependencies": { "dependencies": {
"js-base64": "^3.7.2",
"script_sanitize": "^1.2.6" "script_sanitize": "^1.2.6"
} }
} }

@ -1,5 +1,4 @@
import React, { useMemo } from 'react'; import React, { useMemo } from 'react';
import { encode } from 'js-base64';
import { isValidStr } from '@capital/common'; import { isValidStr } from '@capital/common';
import { Translate } from '../translate'; import { Translate } from '../translate';
import { sanitize } from 'script_sanitize'; import { sanitize } from 'script_sanitize';
@ -18,27 +17,48 @@ function getInjectedStyle() {
} }
} }
const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => { const cacheMap = new Map<string, string>();
const panelInfo = props.panelInfo;
if (!panelInfo) { function createInlineUrl(html: string): string | undefined {
return <div>{Translate.notfound}</div>; if (cacheMap.has(html)) {
return cacheMap.get(html);
} }
const html = panelInfo?.meta?.html;
const url = useMemo(() => {
if (isValidStr(html)) { if (isValidStr(html)) {
const appendHtml = getInjectedStyle(); // 额外追加的样式 const appendHtml = getInjectedStyle(); // 额外追加的样式
try { try {
return `data:text/html;charset=utf8;base64,${encode( const blob = new Blob(
sanitize(html + appendHtml, { replacementText: 'No allowed script' }) [
)}`; sanitize(html + appendHtml, {
replacementText: 'No allowed script',
}),
],
{
type: 'text/html',
}
);
const url = window.URL.createObjectURL(blob);
cacheMap.set(html, url);
return url;
} catch (e) { } catch (e) {
return undefined; return undefined;
} }
} else { } else {
return undefined; return undefined;
} }
}
const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
const panelInfo = props.panelInfo;
if (!panelInfo) {
return <div>{Translate.notfound}</div>;
}
const html = panelInfo?.meta?.html;
const url = useMemo(() => {
return createInlineUrl(html);
}, [html]); }, [html]);
return <WebviewKeepAlive className="w-full h-full" url={url} />; return <WebviewKeepAlive className="w-full h-full" url={url} />;

@ -751,10 +751,8 @@ importers:
client/web/plugins/com.msgbyte.webview: client/web/plugins/com.msgbyte.webview:
specifiers: specifiers:
js-base64: ^3.7.2
script_sanitize: ^1.2.6 script_sanitize: ^1.2.6
dependencies: dependencies:
js-base64: 3.7.2
script_sanitize: 1.2.6 script_sanitize: 1.2.6
client/web/plugins/com.msgbyte.wenshushu: client/web/plugins/com.msgbyte.wenshushu:

Loading…
Cancel
Save