@@ -11576,6 +11576,33 @@ def get_last_fingerprint(self) -> Optional[Dict[str, Any]]:
1157611576 return None
1157711577 return dict(self._last_fingerprint)
1157811578
11579+ async def get_current_user_agent(self) -> Optional[str]:
11580+ """获取当前浏览器实例的真实 User-Agent。
11581+
11582+ 按优先级依次尝试:
11583+ 1) 最近一次打码指纹中的 user_agent
11584+ 2) 初始化时构建的 runtime_surface_profile 中的 userAgent
11585+ 3) 通过 CDP 从运行态浏览器实时获取
11586+ """
11587+ # 1) 从已缓存的浏览器指纹获取
11588+ fingerprint = self.get_last_fingerprint()
11589+ if isinstance(fingerprint, dict) and fingerprint.get("user_agent"):
11590+ return fingerprint["user_agent"]
11591+
11592+ # 2) 从初始化时已构建的 runtime_surface_profile 获取(无需 CDP 调用)
11593+ runtime_profile = self._get_runtime_surface_profile()
11594+ if isinstance(runtime_profile, dict):
11595+ user_agent = runtime_profile.get("userAgent")
11596+ if user_agent:
11597+ return user_agent
11598+
11599+ # 3) 通过 CDP 直接从运行态浏览器获取
11600+ live_ua, _ = await self._get_live_browser_runtime_identity()
11601+ if live_ua:
11602+ return live_ua
11603+
11604+ return None
11605+
1157911606 async def _clear_browser_cache(self):
1158011607 """清理浏览器全部缓存"""
1158111608 if not self.browser:
@@ -13925,6 +13952,36 @@ def get_last_fingerprint(self) -> Optional[Dict[str, Any]]:
1392513952 return fingerprint
1392613953 return None
1392713954
13955+ async def get_current_user_agent(self) -> Optional[str]:
13956+ """获取当前浏览器实例的真实 User-Agent。
13957+
13958+ 按优先级依次从 worker 中尝试:
13959+ 1) 最近一次打码指纹中的 user_agent
13960+ 2) 初始化时构建的 runtime_surface_profile 中的 userAgent
13961+ 3) 通过 CDP 从运行态浏览器实时获取
13962+ """
13963+ # 1) 从已缓存的浏览器指纹获取
13964+ fingerprint = self.get_last_fingerprint()
13965+ if isinstance(fingerprint, dict) and fingerprint.get("user_agent"):
13966+ return fingerprint["user_agent"]
13967+
13968+ # 2) 从已初始化的 worker 的 runtime_surface_profile 获取
13969+ for worker in self._workers:
13970+ runtime_profile = worker._get_runtime_surface_profile()
13971+ if isinstance(runtime_profile, dict):
13972+ user_agent = runtime_profile.get("userAgent")
13973+ if user_agent:
13974+ return user_agent
13975+
13976+ # 3) 通过 CDP 从有活跃浏览器的 worker 实时获取
13977+ for worker in self._workers:
13978+ if worker.browser:
13979+ live_ua, _ = await worker._get_live_browser_runtime_identity()
13980+ if live_ua:
13981+ return live_ua
13982+
13983+ return None
13984+
1392813985 async def get_custom_token(
1392913986 self,
1393013987 website_url: str,
0 commit comments