|
| 1 | +# Bridge Mode by Chrome Extension |
| 2 | + |
| 3 | +import { PackageManagerTabs } from '@theme'; |
| 4 | + |
| 5 | +The bridge mode in the Midscene Chrome extension is a tool that allows you to use local scripts to control the desktop version of Chrome. Your scripts can connect to either a new tab or the currently active tab. |
| 6 | + |
| 7 | +Using the desktop version of Chrome allows you to reuse all cookies, plugins, page status, and everything else you want. You can work with automation scripts to complete your tasks. This mode is commonly referred to as 'man-in-the-loop' in the context of automation. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +:::info Demo Project |
| 12 | +you can check the demo project of bridge mode here: [https://github.com/web-infra-dev/midscene-example/blob/main/bridge-mode-demo](https://github.com/web-infra-dev/midscene-example/blob/main/bridge-mode-demo) |
| 13 | +::: |
| 14 | + |
| 15 | +## Preparation |
| 16 | + |
| 17 | +Install [Midscene extension from Chrome web store](https://chromewebstore.google.com/detail/midscene/gbldofcpkknbggpkmbdaefngejllnief). We will use it later. |
| 18 | + |
| 19 | +## Step 1. install dependencies |
| 20 | + |
| 21 | +<PackageManagerTabs command="install @midscene/web tsx --save-dev" /> |
| 22 | + |
| 23 | +## Step 2. write scripts |
| 24 | + |
| 25 | +Write and save the following code as `./demo-new-tab.ts`. |
| 26 | + |
| 27 | +```typescript |
| 28 | +import { AgentOverChromeBridge } from "@midscene/web/bridge-mode"; |
| 29 | + |
| 30 | +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); |
| 31 | +Promise.resolve( |
| 32 | + (async () => { |
| 33 | + const agent = new AgentOverChromeBridge(); |
| 34 | + |
| 35 | + // This will connect to a new tab on your desktop Chrome |
| 36 | + // remember to start your chrome extension, click 'allow connection' button. Otherwise you will get an timeout error |
| 37 | + await agent.connectNewTabWithUrl("https://www.bing.com"); |
| 38 | + |
| 39 | + // these are the same as normal Midscene agent |
| 40 | + await agent.ai('type "AI 101" and hit Enter'); |
| 41 | + await sleep(3000); |
| 42 | + |
| 43 | + await agent.aiAssert("there are some search results"); |
| 44 | + await agent.destroy(); |
| 45 | + })() |
| 46 | +); |
| 47 | +``` |
| 48 | + |
| 49 | +## Step 3. run |
| 50 | + |
| 51 | +Launch your desktop Chrome. Start Midscene extension and switch to 'Bridge Mode' tab. Click "Allow connection". |
| 52 | + |
| 53 | +Run your scripts |
| 54 | + |
| 55 | +```bash |
| 56 | +tsx demo-new-tab.ts |
| 57 | +``` |
| 58 | + |
| 59 | +After executing the script, you should see the status of the Chrome extension switched to 'connected', and a new tab has been opened. Now this tab is controlled by your scripts. |
| 60 | + |
| 61 | +:::info |
| 62 | +Whether the scripts are run before or after clicking 'Allow connection' in the browser is not significant. |
| 63 | +::: |
| 64 | + |
| 65 | +## API |
| 66 | + |
| 67 | +Except [the normal agent interface](./api), `AgentOverChromeBridge` provides some other interfaces to control the desktop Chrome. |
| 68 | + |
| 69 | +:::info |
| 70 | +You should always call `connectCurrentTab` or `connectNewTabWithUrl` before doing further actions. |
| 71 | + |
| 72 | +Each of the agent instance can only connect to one tab instance, and it cannot be reconnected after destroy. |
| 73 | +::: |
| 74 | + |
| 75 | +### `connectCurrentTab` |
| 76 | + |
| 77 | +Connect to the current active tab on Chrome. |
| 78 | + |
| 79 | +### `connectNewTabWithUrl(ur: string)` |
| 80 | + |
| 81 | +Create a new tab with url and connect to immediately. |
| 82 | + |
| 83 | +### `destroy` |
| 84 | + |
| 85 | +Destroy the connection. |
| 86 | + |
| 87 | +## Use bridge mode in yaml-script |
| 88 | + |
| 89 | +We are still building this, and it will be ready soon. |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
0 commit comments