Skip to content

Commit

Permalink
feat: expose debug mode for main world via content script
Browse files Browse the repository at this point in the history
  • Loading branch information
mondaychen committed Jun 18, 2024
1 parent 1ce0f7d commit 5a6db56
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
40 changes: 25 additions & 15 deletions src/common/TaskUI.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { useCallback, useState } from "react";
import React, { useCallback } from "react";
import {
Button,
Box,
HStack,
Spacer,
Textarea,
useToast,
Alert,
AlertIcon,
Expand All @@ -19,6 +18,19 @@ import TaskStatus from "./TaskStatus";
import RecommendedTasks from "./RecommendedTasks";
import AutosizeTextarea from "./AutosizeTextarea";

const injectContentScript = async () => {
const [tab] = await chrome.tabs.query({ currentWindow: true, active: true });
if (!tab || !tab.id) {
return;
}

await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["src/pages/contentInjected/index.js"],
world: "MAIN",
});
};

function ActionExecutor() {
const state = useAppState((state) => ({
attachDebugger: state.currentTask.actions.attachDebugger,
Expand All @@ -27,28 +39,26 @@ function ActionExecutor() {
prepareLabels: state.currentTask.actions.prepareLabels,
showImagePrompt: state.currentTask.actions.showImagePrompt,
}));
const [action, setAction] = useState<string>(`{
"thought": "try searching",
"action": "click('search')"
}
`);
return (
<Box mt={4}>
<Textarea
value={action}
onChange={(e) => setAction(e.target.value)}
mb={2}
/>
<HStack>
<HStack
columnGap="0.5rem"
rowGap="0.5rem"
fontSize="md"
borderTop="1px dashed gray"
py="3"
shouldWrapChildren
wrap="wrap"
>
<Button onClick={state.attachDebugger}>Attach</Button>
<Button onClick={state.prepareLabels}>Prepare</Button>
<Button onClick={state.showImagePrompt}>Show Image</Button>
<Button
onClick={() => {
state.performActionString(action);
injectContentScript();
}}
>
Run
Inject
</Button>
</HStack>
</Box>
Expand Down
2 changes: 0 additions & 2 deletions src/pages/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@
*
*/
import("@pages/content/injected");

console.log("content loaded");
6 changes: 3 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path, { resolve } from "path";
import makeManifest from "./utils/plugins/make-manifest";
import customDynamicImport from "./utils/plugins/custom-dynamic-import";
import addHmr from "./utils/plugins/add-hmr";
import inlineVitePreloadScript from "./utils/plugins/inline-vite-preload-script";

Check failure on line 7 in vite.config.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module './utils/plugins/inline-vite-preload-script' or its corresponding type declarations.

const rootDir = resolve(__dirname);
const srcDir = resolve(rootDir, "src");
Expand Down Expand Up @@ -32,6 +33,7 @@ export default defineConfig({
react(),
customDynamicImport(),
addHmr({ background: enableHmrInBackgroundScript, view: true }),
inlineVitePreloadScript(),
],
publicDir,
build: {
Expand All @@ -50,10 +52,8 @@ export default defineConfig({
content: resolve(pagesDir, "content", "index.ts"),
contentStyleGlobal: resolve(pagesDir, "content", "style.global.scss"),
contentStyle: resolve(pagesDir, "content", "style.scss"),
contentInjected: resolve(pagesDir, "content/mainWorld", "index.ts"),
permission: resolve(pagesDir, "permission", "index.html"),
// TODO: current cannot support multiple content script entry files
// https://github.com/Jonghakseo/chrome-extension-boilerplate-react-vite/issues/306#issuecomment-1981885190
// mainWorld: resolve(pagesDir, "content/mainWorld", "index.ts"),
popup: resolve(pagesDir, "popup", "index.html"),
newtab: resolve(pagesDir, "newtab", "index.html"),
options: resolve(pagesDir, "options", "index.html"),
Expand Down

0 comments on commit 5a6db56

Please sign in to comment.