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.
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.
you can check the demo project of bridge mode here: https://github.com/web-infra-dev/midscene-example/blob/main/bridge-mode-demo
Write and save the following code as ./demo-new-tab.ts
.
Start the chrome extension and switch to 'Bridge Mode' tab. Click "Allow connection".
Run your scripts
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.
Whether the scripts are run before or after clicking 'Allow connection' in the browser is not significant.
Except the normal parameters in the agent constructor, AgentOverChromeBridge
accepts one more parameter:
closeNewTabsAfterDisconnect?: boolean
: If true, the newly created tab will be closed when the bridge is destroyed. Default is false.Except the normal agent interface, AgentOverChromeBridge
provides some other interfaces to control the desktop Chrome.
You should always call connectCurrentTab
or connectNewTabWithUrl
before doing further actions.
Each of the agent instance can only connect to one tab instance, and it cannot be reconnected after destroy.
connectCurrentTab()
Connect to the currently active tab.
Parameters:
options?: object
- Optional configuration object
forceSameTabNavigation?: boolean
- If true (default), restricts pages from opening new tabs, forcing new pages to open in the current tab to prevent AI operation failures due to manual tab switching. This configuration usually doesn't need to be changedReturns:
Example:
connectNewTabWithUrl()
Create a new tab and connect to it immediately.
Parameters:
url: string
- URL to open in the new taboptions?: object
- Optional configuration object (same parameters as connectCurrentTab)Returns:
Example:
destroy()
Destroy the connection and release resources.
Parameters:
closeNewTabsAfterDisconnect?: boolean
- If true, the newly created tab will be closed when the bridge is destroyed. Default is false. The will override the closeNewTabsAfterDisconnect
parameter in the constructor.Returns:
Example:
Yaml scripts is a way for developers to write automation scripts in yaml format, which is easy to read and write comparing to javascript.
To use bridge mode in yaml script, set the bridgeMode
property in the target
section. If you want to use the current tab, set it to currentTab
, otherwise set it to newTabWithUrl
.
Set closeNewTabsAfterDisconnect
to true if you want to close the newly created tabs when the bridge is destroyed. This is optional and the default value is false.
For example, the following script will open a new tab by Chrome extension bridge:
Run the script:
Remember to start the chrome extension and click 'Allow connection' button after the script is running.
In bridge mode, these options will be ignored (they will follow your desktop browser's settings):
OPENAI_API_KEY
, in the browser or in the terminal?When using bridge mode, you should config the OPENAI_API_KEY
in the terminal.