From b30f575cba5fa5f582a2cdd039bd3e8d5df8a666 Mon Sep 17 00:00:00 2001 From: wangjc683 Date: Sat, 9 May 2026 13:31:37 +0800 Subject: [PATCH] feat: add tabs.create support in bridge extension and TMWebDriver - Add 'create' method handler in background.js tabs command - Add TMWebDriver.newtab() method to open new tabs via bridge - Uses chrome.tabs.create API, bypassing popup blocker - Tested with active/inactive, duplicate URLs, rapid consecutive opens --- TMWebDriver.py | 6 ++++++ assets/tmwd_cdp_bridge/background.js | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/TMWebDriver.py b/TMWebDriver.py index 6a4a704a..9f2d120c 100644 --- a/TMWebDriver.py +++ b/TMWebDriver.py @@ -281,5 +281,11 @@ def set_session(self, url_pattern: str) -> bool: def jump(self, url, timeout=10): self.execute_js(f"window.location.href='{url}'", timeout=timeout) + def newtab(self, url=None, active=True): + """Open a new browser tab via bridge extension (chrome.tabs.create).""" + import json + code = json.dumps({"cmd": "tabs", "method": "create", "url": url or "about:blank", "active": active}) + return self.execute_js(code) + if __name__ == "__main__": driver = TMWebDriver(host='127.0.0.1', port=18765) \ No newline at end of file diff --git a/assets/tmwd_cdp_bridge/background.js b/assets/tmwd_cdp_bridge/background.js index 97fa88ec..9263b09a 100644 --- a/assets/tmwd_cdp_bridge/background.js +++ b/assets/tmwd_cdp_bridge/background.js @@ -25,6 +25,12 @@ async function handleExtMessage(msg, sender) { const tab = await chrome.tabs.update(msg.tabId, { active: true }); await chrome.windows.update(tab.windowId, { focused: true }); return { ok: true }; + } else if (msg.method === 'create') { + const opts = {}; + if (msg.url) opts.url = msg.url; + if (msg.active !== undefined) opts.active = msg.active; + const tab = await chrome.tabs.create(opts); + return { ok: true, data: { id: tab.id, url: tab.url || (msg.url || 'about:blank'), title: tab.title || '', windowId: tab.windowId } }; } else { const tabs = (await chrome.tabs.query({})).filter(t => isScriptable(t.url)); const data = tabs.map(t => ({ id: t.id, url: t.url, title: t.title, active: t.active, windowId: t.windowId }));