当前位置: 首页 > news >正文

在 node 端执行打开一个新的 chrome 并且跳转到指定 url

背景

在 node 端执行打开一个新的 chrome 浏览器 并且跳转到指定 url

注意不是在原有浏览器打开新页签

代码

talking is cheap, show me the code

  @Get('/url')async getUser(@Query('url') url: string): Promise<any> {try {if (!url) {return {success: false,error: 'URL参数不能为空',};}let targetUrl = url;if (!url.startsWith('http://') && !url.startsWith('https://')) {targetUrl = `https://${url}`;}const isWin = process.platform === 'win32';const isMac = process.platform === 'darwin';const isLinux = process.platform === 'linux';let command: string;if (isMac) {command = `osascript -e 'tell application "Google Chrome" to make new window' -e 'tell application "Google Chrome" to set URL of active tab of front window to "${targetUrl}"'`;} else if (isWin) {command = `start chrome --new-window "${targetUrl}"`;} else if (isLinux) {command = `google-chrome --new-window "${targetUrl}"`;} else {return {success: false,error: '不支持的操作系统',};}exec(command, (error, stdout, stderr) => {if (error) {console.error('打开Chrome失败:', error);}if (stderr) {console.error('Chrome错误输出:', stderr);}if (stdout) {console.log('Chrome输出:', stdout);}});return {success: true,url: targetUrl,message: '正在打开Chrome浏览器...',};} catch (error) {return {success: false,error: error.message,};}}
http://www.dtcms.com/a/273295.html

相关文章:

  • 力扣热门算法题 136.只出现一次的数字,139.单词拆分,141.环形链表
  • Docker——容器文件操作
  • 实习第一个小需求样式问题总结
  • 八猴渲染器三维场景实时预览软件 Marmoset Toolbag 5.02b1 Win
  • 20250710-day11
  • 如何选择合适的ai降重工具?七个实用的ai查重网站
  • MyBatis插件机制揭秘:从拦截器开发到分页插件实战
  • 「莫尔物理新范式」普林斯顿马普所合作Nature论文:SnSe₂/ZrS₂扭曲双层实现M点能谷调控与拓扑新效应
  • 安装VMware详细步骤
  • 基于模糊控制及BP神经网络开关磁阻电机的matlab仿真
  • Python-函数进阶
  • 国内如何考取Oracle大师
  • F-GNN的新型检测框架:随机森林增强图神经网络
  • JDK动态代理:深入解析Java动态代理的核心实现
  • qwen3、gemma3 GPRO强化训练案例
  • spring-ai agent概念
  • 6.4 BL2到BL31/BL33的切换
  • Android 13----在framworks层映射一个物理按键
  • C++并发编程-12. 用内存顺序实现内存模型
  • 写《XX顶层设计》和《XX可研报告》区别。
  • MySQL索引:数据库的超级目录
  • 网站文章更新慢影响排名?AI批量写文章技巧分享
  • 综合演练——名片管理系统I
  • Canvas 状态管理 语法糖 canvas.withSave() {}
  • AtCoder Beginner Contest 413
  • 并发编程原理与实战(十六)深入锁的演进,为什么有了synchronized还需要Lock?
  • UECC-UE连接协调的运作方式
  • (一)OpenCV——噪声去除(降噪)
  • React--Fiber 架构
  • 数据库操作核心知识点整理