Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add default plugins to extension bundle #93

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"redux-thunk": "^2.4.2",
"tailwindcss": "^3.3.3",
"tlsn-js": "0.1.0-alpha.6.2",
"tlsn-jsV5.3": "npm:[email protected].3"
"tlsn-js-v5": "npm:[email protected].4"
},
"devDependencies": {
"@babel/core": "^7.20.12",
Expand Down
1 change: 0 additions & 1 deletion plugins/README.md

This file was deleted.

10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added src/assets/plugins/discord_dm.wasm
Binary file not shown.
Binary file added src/assets/plugins/twitter_profile.wasm
Binary file not shown.
5 changes: 3 additions & 2 deletions src/components/PluginInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@
} from '../../utils/plugins';
import { ErrorModal } from '../ErrorModal';
import classNames from 'classnames';
import DefaultPluginIcon from '../../assets/img/default-plugin-icon.png';

export default function PluginUploadInfo(): ReactElement {
const [error, showError] = useState('');
const [pluginBuffer, setPluginBuffer] = useState<ArrayBuffer | any>(null);

Check warning on line 31 in src/components/PluginInfo/index.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const [pluginContent, setPluginContent] = useState<PluginConfig | null>(null);

const onAddPlugin = useCallback(
async (evt: React.MouseEvent<HTMLButtonElement>) => {

Check warning on line 35 in src/components/PluginInfo/index.tsx

View workflow job for this annotation

GitHub Actions / build

'evt' is defined but never used
try {
await addPlugin(Buffer.from(pluginBuffer).toString('hex'));
setPluginContent(null);
} catch (e: any) {

Check warning on line 39 in src/components/PluginInfo/index.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
showError(e?.message || 'Invalid Plugin');
}
},
Expand All @@ -51,7 +52,7 @@
const plugin = await makePlugin(arrayBuffer);
setPluginContent(await getPluginConfig(plugin));
setPluginBuffer(arrayBuffer);
} catch (e: any) {

Check warning on line 55 in src/components/PluginInfo/index.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
showError(e?.message || 'Invalid Plugin');
} finally {
evt.target.value = '';
Expand Down Expand Up @@ -107,11 +108,11 @@
const { pluginContent, onClose, onAddPlugin, children } = props;

const header = Children.toArray(children).filter(
(c: any) => c.type.name === 'PluginInfoModalHeader',

Check warning on line 111 in src/components/PluginInfo/index.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
)[0];

const content = Children.toArray(children).filter(
(c: any) => c.type.name === 'PluginInfoModalContent',

Check warning on line 115 in src/components/PluginInfo/index.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
)[0];

return (
Expand All @@ -122,7 +123,7 @@
<ModalHeader className="w-full p-2 border-gray-200 text-gray-500">
{header || (
<div className="flex flex-row items-end justify-start gap-2">
<img className="h-5" src={logo} alt="logo" />
<img className="h-5" src={logo || DefaultPluginIcon} alt="logo" />
<span className="font-semibold">{`Installing ${pluginContent.title}`}</span>
</div>
)}
Expand All @@ -132,7 +133,7 @@
<>
<img
className="w-12 h-12"
src={pluginContent.icon}
src={pluginContent.icon || DefaultPluginIcon}
alt="Plugin Icon"
/>
<span className="text-3xl text-center">
Expand Down
2 changes: 1 addition & 1 deletion src/components/PluginList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import {
fetchPluginHashes,
removePlugin,
fetchPluginConfigByHash,

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

View workflow job for this annotation

GitHub Actions / build

'fetchPluginConfigByHash' is defined but never used
runPlugin,
} from '../../utils/rpc';
import { usePluginHashes } from '../../reducers/plugins';
Expand Down Expand Up @@ -75,7 +75,7 @@
if (chrome.sidePanel) await chrome.sidePanel.open({ tabId: tab.id });

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

Check warning on line 78 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]);
Expand Down Expand Up @@ -169,7 +169,7 @@
<PluginInfoModalContent className="flex flex-col items-center cursor-default">
<img
className="w-12 h-12 mb-2"
src={config.icon}
src={config.icon || DefaultPluginIcon}
alt="Plugin Icon"
/>
<span className="text-3xl text-blue-600 font-semibold">
Expand Down
22 changes: 22 additions & 0 deletions src/entries/Background/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ const cookiesDb = db.sublevel<string, boolean>('cookies', {
const headersDb = db.sublevel<string, boolean>('headers', {
valueEncoding: 'json',
});
const appDb = db.sublevel<string, any>('app', {
valueEncoding: 'json',
});
enum AppDatabaseKey {
DefaultPluginsInstalled = 'DefaultPluginsInstalled',
}

export async function addNotaryRequest(
now = Date.now(),
Expand Down Expand Up @@ -372,3 +378,19 @@ export async function getHeadersByHost(host: string) {
}
return ret;
}

async function getDefaultPluginsInstalled(): Promise<boolean> {
return appDb.get(AppDatabaseKey.DefaultPluginsInstalled).catch(() => false);
}

export async function setDefaultPluginsInstalled(installed = false) {
return mutex.runExclusive(async () => {
await appDb.put(AppDatabaseKey.DefaultPluginsInstalled, installed);
});
}

export async function getAppState() {
return {
defaultPluginsInstalled: await getDefaultPluginsInstalled(),
};
}
15 changes: 15 additions & 0 deletions src/entries/Background/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { onBeforeRequest, onResponseStarted, onSendHeaders } from './handlers';
import { deleteCacheByTabId } from './cache';
import browser from 'webextension-polyfill';
import { getAppState, setDefaultPluginsInstalled } from './db';
import { installPlugin } from './plugins/utils';

(async () => {
browser.webRequest.onSendHeaders.addListener(
Expand Down Expand Up @@ -31,6 +33,19 @@ import browser from 'webextension-polyfill';
deleteCacheByTabId(tabId);
});

const { defaultPluginsInstalled } = await getAppState();

if (!defaultPluginsInstalled) {
try {
const twitterProfileUrl = browser.runtime.getURL('twitter_profile.wasm');
const discordDmUrl = browser.runtime.getURL('discord_dm.wasm');
await installPlugin(twitterProfileUrl);
await installPlugin(discordDmUrl);
} finally {
await setDefaultPluginsInstalled(true);
}
}

const { initRPC } = await import('./rpc');
await createOffscreenDocument();
initRPC();
Expand Down
29 changes: 29 additions & 0 deletions src/entries/Background/plugins/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { addPlugin, addPluginConfig, addPluginMetadata } from '../db';
import { getPluginConfig } from '../../../utils/misc';

export async function installPlugin(
urlOrBuffer: ArrayBuffer | string,
origin = '',
filePath = '',
metadata: {[key: string]: string} = {},
) {
let arrayBuffer;

if (typeof urlOrBuffer === 'string') {
const resp = await fetch(urlOrBuffer);
arrayBuffer = await resp.arrayBuffer();
} else {
arrayBuffer = urlOrBuffer;
}

const config = await getPluginConfig(arrayBuffer);
const hex = Buffer.from(arrayBuffer).toString('hex');
const hash = await addPlugin(hex);
await addPluginConfig(hash!, config);
await addPluginMetadata(hash!, {
...metadata,
origin,
filePath,
});
return hash;
}
10 changes: 10 additions & 0 deletions src/entries/Background/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
getPlugins,
getCookiesByHost,
getHeadersByHost,
getAppState,
setDefaultPluginsInstalled,
} from './db';
import { addOnePlugin, removeOnePlugin } from '../../reducers/plugins';
import {
Expand Down Expand Up @@ -85,6 +87,8 @@ export enum BackgroundActiontype {
run_plugin_request = 'run_plugin_request',
run_plugin_response = 'run_plugin_response',
get_logging_level = 'get_logging_level',
get_app_state = 'get_app_state',
set_default_plugins_installed = 'set_default_plugins_installed',
}

export type BackgroundAction = {
Expand Down Expand Up @@ -192,6 +196,12 @@ export const initRPC = () => {
case BackgroundActiontype.get_logging_level:
getLoggingFilter().then(sendResponse);
return true;
case BackgroundActiontype.get_app_state:
getAppState().then(sendResponse);
return true;
case BackgroundActiontype.set_default_plugins_installed:
setDefaultPluginsInstalled(request.data).then(sendResponse);
return true;
default:
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/entries/Offscreen/Offscreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
NotarizedSession as _NotarizedSession,
TlsProof as _TlsProof,
} from 'tlsn-js';
import { verify } from 'tlsn-jsV5.3';
import { verify } from 'tlsn-js-v5';

import { urlify } from '../../utils/misc';
import { BackgroundActiontype } from '../Background/rpc';
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
],
"web_accessible_resources": [
{
"resources": ["content.styles.css", "icon-128.png", "icon-34.png", "content.bundle.js"],
"resources": ["content.styles.css", "icon-128.png", "icon-34.png", "content.bundle.js", "discord_dm.wasm", "twitter_profile.wasm"],
"matches": ["http://*/*", "https://*/*", "<all_urls>"]
}
],
Expand Down
33 changes: 12 additions & 21 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ var options = {
mode: process.env.NODE_ENV || "development",
ignoreWarnings: [
/Circular dependency between chunks with runtime/,
/ResizeObserver loop completed with undelivered notifications/
/ResizeObserver loop completed with undelivered notifications/,
/Should not import the named export/,
],

entry: {
Expand Down Expand Up @@ -199,31 +200,21 @@ var options = {
}),
new CopyWebpackPlugin({
patterns: [
// {
// from: "node_modules/tlsn-js/build/7.js",
// to: path.join(__dirname, "build"),
// force: true,
// },
// {
// from: "node_modules/tlsn-js/build/250.js",
// to: path.join(__dirname, "build"),
// force: true,
// },
// {
// from: "node_modules/tlsn-js/build/278.js",
// to: path.join(__dirname, "build"),
// force: true,
// },
// {
// from: "node_modules/tlsn-js/build/349.js",
// to: path.join(__dirname, "build"),
// force: true,
// },
{
from: "node_modules/tlsn-js/build",
to: path.join(__dirname, "build"),
force: true,
},
{
from: "src/assets/plugins/discord_dm.wasm",
to: path.join(__dirname, "build"),
force: true,
},
{
from: "src/assets/plugins/twitter_profile.wasm",
to: path.join(__dirname, "build"),
force: true,
},
],
}),
new HtmlWebpackPlugin({
Expand Down
Loading