Skip to content

Commit

Permalink
Merge branch 'main' into hostfunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
Codetrauma authored Dec 2, 2024
2 parents ea28041 + 4c908d0 commit cad2d9f
Show file tree
Hide file tree
Showing 30 changed files with 2,524 additions and 246 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tlsn-extension",
"version": "0.1.0.700",
"version": "0.1.0.702",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/components/PluginList/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
padding: 0;
overflow: hidden;
transition: 200ms opacity;

transition-delay: 200ms;
}

&:hover {
Expand Down
100 changes: 69 additions & 31 deletions src/components/PluginList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import React, {
} from 'react';
import { fetchPluginHashes, removePlugin, runPlugin } from '../../utils/rpc';
import { usePluginHashes } from '../../reducers/plugins';
import { PluginConfig } from '../../utils/misc';
import {
getPluginConfig,
hexToArrayBuffer,
PluginConfig,
} from '../../utils/misc';
import DefaultPluginIcon from '../../assets/img/default-plugin-icon.png';
import classNames from 'classnames';
import Icon from '../Icon';
Expand All @@ -20,90 +24,121 @@ import {
PluginInfoModalHeader,
} from '../PluginInfo';
import { getPluginConfigByHash } from '../../entries/Background/db';

export function PluginList(props: { className?: string }): ReactElement {
import { OffscreenActionTypes } from '../../entries/Offscreen/types';

Check warning on line 27 in src/components/PluginList/index.tsx

View workflow job for this annotation

GitHub Actions / build

'OffscreenActionTypes' is defined but never used
import { SidePanelActionTypes } from '../../entries/SidePanel/types';
import { openSidePanel } from '../../entries/utils';

export function PluginList({
className,
unremovable,
onClick,
}: {
className?: string;
unremovable?: boolean;
onClick?: (hash: string) => void;
}): ReactElement {
const hashes = usePluginHashes();

useEffect(() => {
fetchPluginHashes();
}, []);

return (
<div
className={classNames('flex flex-col flex-nowrap gap-1', props.className)}
>
<div className={classNames('flex flex-col flex-nowrap gap-1', className)}>
{!hashes.length && (
<div className="flex flex-col items-center justify-center text-slate-400 cursor-default select-none">
<div>No available plugins</div>
</div>
)}
{hashes.map((hash) => (
<Plugin key={hash} hash={hash} />
<Plugin
key={hash}
hash={hash}
unremovable={unremovable}
onClick={onClick}
/>
))}
</div>
);
}

export function Plugin(props: {
export function Plugin({
hash,
hex,
unremovable,
onClick,
className,
}: {
hash: string;
onClick?: () => void;
hex?: string;
className?: string;
onClick?: (hash: string) => void;
unremovable?: boolean;
}): ReactElement {
const [error, showError] = useState('');
const [config, setConfig] = useState<PluginConfig | null>(null);
const [pluginInfo, showPluginInfo] = useState(false);
const [remove, showRemove] = useState(false);

const onClick = useCallback(async () => {
const onRunPlugin = useCallback(async () => {
if (!config || remove) return;

if (onClick) {
onClick(hash);
return;
}

try {
await runPlugin(props.hash, 'start');
await openSidePanel();

const [tab] = await browser.tabs.query({
active: true,
currentWindow: true,
browser.runtime.sendMessage({
type: SidePanelActionTypes.execute_plugin_request,
data: {
pluginHash: hash,
},
});

await browser.storage.local.set({ plugin_hash: props.hash });

// @ts-ignore
if (chrome.sidePanel) await chrome.sidePanel.open({ tabId: tab.id });
await runPlugin(hash, 'start');

window.close();
} catch (e: any) {

Check warning on line 104 in src/components/PluginList/index.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
showError(e.message);
}
}, [props.hash, config, remove]);
}, [hash, config, remove, onClick]);

useEffect(() => {
(async function () {
setConfig(await getPluginConfigByHash(props.hash));
if (hex) {
setConfig(await getPluginConfig(hexToArrayBuffer(hex)));
} else {
setConfig(await getPluginConfigByHash(hash));
}
})();
}, [props.hash]);
}, [hash, hex]);

const onRemove: MouseEventHandler = useCallback(
(e) => {
e.stopPropagation();
removePlugin(props.hash);
removePlugin(hash);
showRemove(false);
},
[props.hash, remove],
[hash, remove],
);

const onConfirmRemove: MouseEventHandler = useCallback(
(e) => {
e.stopPropagation();
showRemove(true);
},
[props.hash, remove],
[hash, remove],
);

const onPluginInfo: MouseEventHandler = useCallback(
(e) => {
e.stopPropagation();
showPluginInfo(true);
},
[props.hash, pluginInfo],
[hash, pluginInfo],
);

if (!config) return <></>;
Expand All @@ -113,8 +148,9 @@ export function Plugin(props: {
className={classNames(
'flex flex-row justify-center border rounded border-slate-300 p-2 gap-2 plugin-box',
'cursor-pointer hover:bg-slate-100 hover:border-slate-400 active:bg-slate-200',
className,
)}
onClick={onClick}
onClick={onRunPlugin}
>
{!!error && <ErrorModal onClose={() => showError('')} message={error} />}
{!remove ? (
Expand All @@ -129,11 +165,13 @@ export function Plugin(props: {
className="flex flex-row items-center justify-center cursor-pointer plugin-box__remove-icon"
onClick={onPluginInfo}
/>
<Icon
fa="fa-solid fa-xmark"
className="flex flex-row items-center justify-center cursor-pointer text-red-500 bg-red-200 rounded-full plugin-box__remove-icon"
onClick={onConfirmRemove}
/>
{!unremovable && (
<Icon
fa="fa-solid fa-xmark"
className="flex flex-row items-center justify-center cursor-pointer text-red-500 bg-red-200 rounded-full plugin-box__remove-icon"
onClick={onConfirmRemove}
/>
)}
</div>
</div>
<div>{config.description}</div>
Expand Down
14 changes: 10 additions & 4 deletions src/entries/Background/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,16 @@ export async function getPlugins(): Promise<
ret.push({
...config,
hash,
metadata: metadata || {
filePath: '',
origin: '',
},
metadata: metadata
? {
...metadata,
hash,
}
: {
filePath: '',
origin: '',
hash,
},
});
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/entries/Background/plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ export async function installPlugin(
filePath,
});
return hash;
}

export function mapSecretsToRange(secrets: string[], text: string) {
return secrets
.map((secret: string) => {
const index = text.indexOf(secret);
return index > -1
? {
start: index,
end: index + secret.length,
}
: null;
})
.filter((data: any) => !!data) as { start: number; end: number }[]
}
Loading

0 comments on commit cad2d9f

Please sign in to comment.