refactor: use file stream read and write instead of blobUrl to render custom html panel

pull/90/head
moonrailgun 2 years ago
parent fa498867c4
commit 9965a54bdb

@ -4,7 +4,6 @@
"version": "0.0.0", "version": "0.0.0",
"private": true, "private": true,
"dependencies": { "dependencies": {
"script_sanitize": "^1.2.6",
"url-regex": "^5.0.0" "url-regex": "^5.0.0"
} }
} }

@ -1,8 +1,5 @@
import React, { useMemo } from 'react'; import React, { useEffect, useRef } from 'react';
import { isValidStr } from '@capital/common';
import { Translate } from '../translate'; import { Translate } from '../translate';
import { sanitize } from 'script_sanitize';
import { WebviewKeepAlive } from '@capital/component';
function getInjectedStyle() { function getInjectedStyle() {
try { try {
@ -17,51 +14,27 @@ function getInjectedStyle() {
} }
} }
const cacheMap = new Map<string, string>(); const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
const panelInfo = props.panelInfo;
function createInlineUrl(html: string): string | undefined { const ref = useRef<HTMLIFrameElement>(null);
if (cacheMap.has(html)) { const html = panelInfo?.meta?.html;
return cacheMap.get(html);
}
if (isValidStr(html)) {
const appendHtml = getInjectedStyle(); // 额外追加的样式
try {
const blob = new Blob(
[
sanitize(html + appendHtml, {
replacementText: 'No allowed script',
}),
],
{
type: 'text/html',
}
);
const url = window.URL.createObjectURL(blob); useEffect(() => {
cacheMap.set(html, url); if (!ref.current || !html) {
return url; return;
} catch (e) {
return undefined;
} }
} else {
return undefined;
}
}
const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => { const doc = ref.current.contentWindow.document;
const panelInfo = props.panelInfo; doc.open();
doc.writeln(getInjectedStyle(), html);
doc.close();
}, [html]);
if (!panelInfo) { if (!panelInfo) {
return <div>{Translate.notfound}</div>; return <div>{Translate.notfound}</div>;
} }
const html = panelInfo?.meta?.html; return <iframe ref={ref} />;
const url = useMemo(() => {
return createInlineUrl(html);
}, [html]);
return <WebviewKeepAlive className="w-full h-full" url={url} />;
}; };
GroupCustomWebPanelRender.displayName = 'GroupCustomWebPanelRender'; GroupCustomWebPanelRender.displayName = 'GroupCustomWebPanelRender';

@ -1201,9 +1201,6 @@ importers:
client/web/plugins/com.msgbyte.webview: client/web/plugins/com.msgbyte.webview:
dependencies: dependencies:
script_sanitize:
specifier: ^1.2.6
version: 1.2.6
url-regex: url-regex:
specifier: ^5.0.0 specifier: ^5.0.0
version: 5.0.0 version: 5.0.0
@ -31954,10 +31951,6 @@ packages:
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
dev: false dev: false
/script_sanitize@1.2.6:
resolution: {integrity: sha512-CA53o9tyResT5Sq8HT4w6jcmHoYtfbBxRKvaBQc0Iw0kS4+c9MDNwl1ReUcFjbb/GFm8YjB7diTJE10MWOnHNQ==}
dev: false
/scroll-into-view-if-needed@2.2.20: /scroll-into-view-if-needed@2.2.20:
resolution: {integrity: sha512-P9kYMrhi9f6dvWwTGpO5I3HgjSU/8Mts7xL3lkoH5xlewK7O9Obdc5WmMCzppln7bCVGNmf3qfoZXrpCeyNJXw==} resolution: {integrity: sha512-P9kYMrhi9f6dvWwTGpO5I3HgjSU/8Mts7xL3lkoH5xlewK7O9Obdc5WmMCzppln7bCVGNmf3qfoZXrpCeyNJXw==}
dependencies: dependencies:

Loading…
Cancel
Save