in computers/default/browserbase.py [0:0]
def _get_browser_and_page(self) -> Tuple[Browser, Page]:
"""
Create a Browserbase session and connect to it.
Returns:
Tuple[Browser, Page]: A tuple containing the connected browser and page objects.
"""
# Create a session on Browserbase with specified parameters
width, height = self.dimensions
session_params = {
"project_id": self.project_id,
"browser_settings": {
"viewport": {"width": width, "height": height},
"blockAds": self.ad_blocker,
},
"region": self.region,
"proxies": self.proxy,
}
self.session = self.bb.sessions.create(**session_params)
# Print the live session URL
print(
f"Watch and control this browser live at https://www.browserbase.com/sessions/{self.session.id}"
)
# Connect to the remote session
browser = self._playwright.chromium.connect_over_cdp(
self.session.connect_url, timeout=60000
)
context = browser.contexts[0]
# Add event listeners for page creation and closure
context.on("page", self._handle_new_page)
# Only add the init script if virtual_mouse is True
if self.virtual_mouse:
context.add_init_script(
"""
// Only run in the top frame
if (window.self === window.top) {
function initCursor() {
const CURSOR_ID = '__cursor__';
// Check if cursor element already exists
if (document.getElementById(CURSOR_ID)) return;
const cursor = document.createElement('div');
cursor.id = CURSOR_ID;
Object.assign(cursor.style, {
position: 'fixed',
top: '0px',
left: '0px',
width: '20px',
height: '20px',
backgroundImage: 'url("data:image/svg+xml;utf8,<svg xmlns=\\'http://www.w3.org/2000/svg\\' viewBox=\\'0 0 24 24\\' fill=\\'black\\' stroke=\\'white\\' stroke-width=\\'1\\' stroke-linejoin=\\'round\\' stroke-linecap=\\'round\\'><polygon points=\\'2,2 2,22 8,16 14,22 17,19 11,13 20,13\\'/></svg>")',
backgroundSize: 'cover',
pointerEvents: 'none',
zIndex: '99999',
transform: 'translate(-2px, -2px)',
});
document.body.appendChild(cursor);
document.addEventListener("mousemove", (e) => {
cursor.style.top = e.clientY + "px";
cursor.style.left = e.clientX + "px";
});
}
// Use requestAnimationFrame for early execution
requestAnimationFrame(function checkBody() {
if (document.body) {
initCursor();
} else {
requestAnimationFrame(checkBody);
}
});
}
"""
)
page = context.pages[0]
page.on("close", self._handle_page_close)
page.goto("https://bing.com")
return browser, page