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

ブラウザ拡張機能の追加 #460

Merged
merged 23 commits into from
Apr 23, 2024
Merged

ブラウザ拡張機能の追加 #460

merged 23 commits into from
Apr 23, 2024

Conversation

wadabee
Copy link
Contributor

@wadabee wadabee commented Apr 19, 2024

Issue #, if available:
close #448

Description of changes:

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Comment on lines +9 to +12
paths-ignore: ['browser-extension/**']
pull_request:
branches: ['main']
paths-ignore: ['browser-extension/**']
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

拡張機能とそれ以外で、CIを分けました

Comment on lines +10 to +14
このリポジトリではブラウザ拡張機能も提供しており、より便利に Generative AI を活用することができます。詳しくは[こちらのページ](/browser-extension/README.md)をご覧ください。

![拡張機能](/imgs/extension/extension_demo.png)


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

拡張機能の存在に気づかないと思うので、トップページにも追加しました。
超一等地なので問題ありましたら、ご指摘くださいmm

Comment on lines +3 to +4
Copyright (c) 2023 Sinan Bekar
Modifications Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下のRepoを元に作成したので、元のLICENSEファイルを残しています
https://github.com/sinanbekar/browser-extension-react-typescript-starter

Comment on lines +16 to +23
const [tabId, setTabId] = useState<number>(-1);
Browser.tabs?.getCurrent().then((tab) => {
if (tab) {
if (tab.id !== tabId) {
setTabId(tab.id ?? -1);
}
}
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ブラウザのタブごとに状態を管理しています。

Comment on lines +7 to +11
export type ChatState = {
[tabId: number]: {
messages: Message[];
};
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ブラウザのタブごとに状態を管理しています。

Comment on lines +1 to +51
import { wrapStore } from '@eduardoac-skimlinks/webext-redux';
import { combineReducers, configureStore, type Action, type ThunkAction } from '@reduxjs/toolkit';
import { localStorage } from 'redux-persist-webextension-storage';
import {
FLUSH,
PAUSE,
PERSIST,
persistReducer,
persistStore,
PURGE,
REGISTER,
REHYDRATE,
} from 'reduxjs-toolkit-persist';
import type { WebStorage } from 'reduxjs-toolkit-persist/lib/types';

import chatReducer from './features/chat/chatSlice';

const persistConfig = {
key: 'root',
storage: localStorage as WebStorage,
};

const reducers = combineReducers({
chat: chatReducer,
});

const persistedReducer: typeof reducers = persistReducer(persistConfig, reducers);
const store = configureStore({
reducer: persistedReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}),
});

export const initializeWrappedStore = () => wrapStore(store);

export const persistor = persistStore(store);

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
export type AppThunk<ReturnType = void> = ThunkAction<
ReturnType,
RootState,
unknown,
Action<string>
>;

export default store;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ブラウザ拡張機能でReduxを使うための処理です。
メインの方で使っているZustandは当該のライブラリが存在しなかったので、拡張機能はReduxを利用しています。

export default defineConfig({
// @see https://github.com/crxjs/chrome-extension-tools/issues/696
server: {
port: 5174,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メインと衝突しないようにportを変えています。

Comment on lines +49 to +53
<React.StrictMode>
<Provider store={proxyStore}>
<Content />
</Provider>
</React.StrictMode>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この部分がブラウジング中のWebページに埋め込まれます。

Comment on lines +38 to +39
const tw = twind(config, sheet);
observe(tw, shadowRoot);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tailwindをブラウジング中のWebページに埋め込むことができないので、twindという仕組みを使ってRawなStyleに変換して埋め込むようにしています。

Comment on lines +44 to +51
<iframe
ref={iframe}
className={twMerge(
'fixed z-[9999999999999] right-0 top-0 h-dvh shadow-xl border-1 bg-aws-squid-ink transition-all',
isOpenChat ? 'xl:w-1/3 lg:2/5 md:w-2/5 sm:w-2/3' : 'w-0',
)}
src={Browser.runtime.getURL('pages/chat/index.html')}
/>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

右側に表示されるDrawer的な画面はiframeを利用しています。
iframeを利用することで、CSSやJSを自由に使うことができます。
iframeを利用しない場合は、外部のCSSファイルをimportできないため、amplify ui のauthenticatorのスタイリングがでいないという問題がありました。

const manifest = defineManifest(async (env) => ({
manifest_version: 3,
name: `${env.mode === 'development' ? '[Dev] ' : ''}generative-ai-use-cases-jp 拡張機能`,
description: 'generative-ai-use-case-jp をブラウザ拡張機能として利用できます。',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use-cases


事前準備ができたら、以下の手順でビルドを行なってください。

以降の手順は、**必ず `generative-ai-use-case-jp` のルートフォルダで実行してください。`generative-ai-use-case-jp/browser-extension` ではありませんのでご注意ください。**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use-cases

Copy link
Contributor

@tbrand tbrand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!!

@wadabee wadabee merged commit 225a7b5 into main Apr 23, 2024
3 checks passed
@wadabee wadabee deleted the feature/browser-extension branch April 23, 2024 01:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ブラウザ拡張の main マージ
2 participants