web页面如何打开本地终端程序

1.主要思想

通过自定义协议实现

 

2.windows环境

以打开本地opencode为例,就是打开cmd,执行opencode命令。命令还可以带参数,通过bat脚本实现更复杂的命令

2.1.新建opencode.reg注册表文件,点击运行注册

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\opencode]
@="URL:opencode Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\opencode\shell\open\command]
@="cmd /c start opencode"

2.2.新建一个html文件,打开文件,点击按钮即可。或者浏览器输入opencode://,再按回车

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>opencode 启动器</title>
</head>
<body>
    <button onclick="opencode()">打开 opencode</button>

    <script>
        function opencode() {
            window.location.href = 'opencode://';
        }
    </script>
</body>
</html>

 

3.linux环境(麒麟)

以打开本地opencode为例,就是打开终端,执行opencode命令。命令还可以带参数,通过bash脚本实现更复杂的命令

3.1.新建opencode.desktop文件

去/usr/share/applications目录下新建,注意不同操作系统的默认终端不一样,需要修改x-terminal-emulator

[Desktop Entry]
Type=Application
Name=opencode Protocol Handler
Exec=x-terminal-emulator -e bash -c "opencode"
MimeType=x-scheme-handler/opencode;
NoDisplay=true
Terminal=false

执行下面的命令

update-desktop-database;

xdg-mime default opencode.desktop x-scheme-handler/opencode;

xdg-open "opencode://"

 

3.2.新建一个html文件,打开文件,点击按钮即可。或者浏览器输入opencode://,再按回车

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>opencode 启动器</title>
</head>
<body>
    <button onclick="opencode()">打开 opencode</button>

    <script>
        function opencode() {
            window.location.href = 'opencode://';
        }
    </script>
</body>
</html>

 

4.macos环境

注册到Info.plist文件中

 

参考连接:

https://www.electronjs.org/zh/docs/latest/tutorial/launch-app-from-url-in-another-app

https://comate.baidu.com/zh/page/8uch9692bec#0

posted @ 2026-04-01 16:54  wdgde  阅读(58)  评论(0)    收藏  举报