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
.
Launch your desktop Chrome. Start Midscene 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 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(options?: { trackingActiveTab?: boolean })
Connect to the current active tab on Chrome.
If trackingActiveTab
is true, the agent will always track the active tab. For example, if you switch to another tab or a new tab is opened, the agent will track the latest active tab. Otherwise, the agent will only track the tab you connected to initially.
connectNewTabWithUrl(url: string, options?: { trackingActiveTab?: boolean })
Create a new tab with url and connect to immediately.
destroy
Destroy the connection.
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
.
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):
userAgent
viewportWidth
viewportHeight
viewportScale
waitForNetworkIdle
cookie