-
Notifications
You must be signed in to change notification settings - Fork 135
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
ブラウザ拡張機能の追加 #460
Conversation
paths-ignore: ['browser-extension/**'] | ||
pull_request: | ||
branches: ['main'] | ||
paths-ignore: ['browser-extension/**'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
拡張機能とそれ以外で、CIを分けました
このリポジトリではブラウザ拡張機能も提供しており、より便利に Generative AI を活用することができます。詳しくは[こちらのページ](/browser-extension/README.md)をご覧ください。 | ||
|
||
![拡張機能](/imgs/extension/extension_demo.png) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
拡張機能の存在に気づかないと思うので、トップページにも追加しました。
超一等地なので問題ありましたら、ご指摘くださいmm
Copyright (c) 2023 Sinan Bekar | ||
Modifications Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
There was a problem hiding this comment.
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
const [tabId, setTabId] = useState<number>(-1); | ||
Browser.tabs?.getCurrent().then((tab) => { | ||
if (tab) { | ||
if (tab.id !== tabId) { | ||
setTabId(tab.id ?? -1); | ||
} | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ブラウザのタブごとに状態を管理しています。
export type ChatState = { | ||
[tabId: number]: { | ||
messages: Message[]; | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ブラウザのタブごとに状態を管理しています。
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; |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
メインと衝突しないようにportを変えています。
<React.StrictMode> | ||
<Provider store={proxyStore}> | ||
<Content /> | ||
</Provider> | ||
</React.StrictMode>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この部分がブラウジング中のWebページに埋め込まれます。
const tw = twind(config, sheet); | ||
observe(tw, shadowRoot); |
There was a problem hiding this comment.
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に変換して埋め込むようにしています。
<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')} | ||
/> |
There was a problem hiding this comment.
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のスタイリングがでいないという問題がありました。
browser-extension/src/manifest.ts
Outdated
const manifest = defineManifest(async (env) => ({ | ||
manifest_version: 3, | ||
name: `${env.mode === 'development' ? '[Dev] ' : ''}generative-ai-use-cases-jp 拡張機能`, | ||
description: 'generative-ai-use-case-jp をブラウザ拡張機能として利用できます。', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use-cases
docs/EXTENSION_BUILD.md
Outdated
|
||
事前準備ができたら、以下の手順でビルドを行なってください。 | ||
|
||
以降の手順は、**必ず `generative-ai-use-case-jp` のルートフォルダで実行してください。`generative-ai-use-case-jp/browser-extension` ではありませんのでご注意ください。** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use-cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!!!
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.