

嗨 Win10,这是我的浏览器
source link: https://blog.lilydjwg.me/2018/1/18/hi-win10-this-is-my-browser.211960.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

嗨 Win10,这是我的浏览器
本文来自依云's Blog,转载请注明。
因为腾讯的关系,不得不使用 Windows。不是QQ也不是RTX,这次 Wine 帮不上忙了。那好吧,SSD + 16G 内存,跑个 Windows 虚拟机也没多大问题。
但是问题来了:我在虚拟机里每次点击链接,会调用安装于 Windows 上的浏览器来访问。可是我登录了各种工作账号的浏览器在外面的自由世界 Linux 主系统里啊。
办法也好想:来一个远程调用,让 Windows 调用咱自己的程序打开链接,然后咱自己的程序再把链接传给外面的 Linux 就好了。理想是美满的,可是 Windows 里设置默认浏览器并不是写个 .desktop 文件那么简单!
在 Google 上找了半天资料(真的花掉了半天!),最终终于确认,需要设置以下一大堆注册表键,Windows 才会认可咱自己的浏览器:
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\RemoteBrowser] @="RemoteBrowser" [HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\RemoteBrowser\Capabilities] "ApplicationName"="RemoteBrowser" "ApplicationIcon"="C:\\RemoteBrowser\\launch.exe,0" "ApplicationDescription"="RemoteBrowser" [HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\RemoteBrowser\Capabilities\StartMenu] "StartMenuInternet"="RemoteBrowser" [HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\RemoteBrowser\Capabilities\URLAssociations] "https"="RemoteBrowserHTML" "http"="RemoteBrowserHTML" "ftp"="RemoteBrowserHTML" [HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\RemoteBrowser\DefaultIcon] @="C:\\RemoteBrowser\\launch.exe,0" [HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\RemoteBrowser\InstallInfo] "IconsVisible"=dword:00000001 "ShowIconsCommand"="\"C:\\RemoteBrowser\\launch.exe\" --show-icons" "HideIconsCommand"="\"C:\\RemoteBrowser\\launch.exe\" --hide-icons" "ReinstallCommand"="\"C:\\RemoteBrowser\\launch.exe\" --make-default-browser" [HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\RemoteBrowser\shell] [HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\RemoteBrowser\shell\open] [HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\RemoteBrowser\shell\open\command] @="\"C:\\RemoteBrowser\\launch.exe\" \"%1\"" [HKEY_LOCAL_MACHINE\SOFTWARE\RegisteredApplications] "RemoteBrowser"="SOFTWARE\\Clients\\StartMenuInternet\\RemoteBrowser\\Capabilities" [HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations] [HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http] [HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice] "Progid"="RemoteBrowserHTML" [HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https] [HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice] "Progid"="RemoteBrowserHTML" [HKEY_CLASSES_ROOT\RemoteBrowserHTML] @="RemoteBrowser" "FriendlyTypeName"="RemoteBrowser" "URL Protocol"="" "EditFlags"=dword:00000002 [HKEY_CLASSES_ROOT\RemoteBrowserHTML\DefaultIcon] @="C:\\RemoteBrowser\\launch.exe,0" [HKEY_CLASSES_ROOT\RemoteBrowserHTML\shell] @="open" [HKEY_CLASSES_ROOT\RemoteBrowserHTML\shell\open] [HKEY_CLASSES_ROOT\RemoteBrowserHTML\shell\open\command] @="\"C:\\RemoteBrowser\\launch.exe\" \"%1\""
相比之下 Linux 只需要以下这么几行:
Version=1.0 Name=Firefox GenericName=Web Browser Comment=Browse the Web Exec=/usr/lib/firefox/firefox %u Icon=firefox Terminal=false Type=Application MimeType=text/html;text/xml;application/xhtml+xml;application/vnd.mozilla.xul+xml;text/mml;x-scheme-handler/http;x-scheme-handler/https; StartupNotify=true StartupWMClass=Firefox Categories=Network;WebBrowser; Keywords=web;browser;internet;
(不过真实的 firefox.desktop 有三百多行,因为各种语言的名字和描述的翻译。)
上边那个注册表文件,引用了一个 exe 文件,和里边的一个图标资源。我不知道图标使用独立的文件可不可以。加个图标并不难,上网搜一下就有了,使用 windres 命令即可。
首先准备好图标文件 icon.ico,然后写一个资源文件:
1 ICON DISCARDABLE "icon.ico"
用 windres 把它编译成 COFF 文件。因为我是在 Linux 上使用 mingw 操作,所以使用的命令叫 x86_64-w64-mingw32-windres。然后把它和其它目标文件链接到一起就可以了。因为我使用的是 Rust,它的 build.rs 脚本不支持目标文件,所以先打包成静态库,然后再链接:
x86_64-w64-mingw32-windres launch.rc -O coff -o icon.res
x86_64-w64-mingw32-ar q libres.a icon.res
然后 build.rs 脚本里说一下:
fn main() { println!("cargo:rustc-link-search=native=.."); println!("cargo:rustc-link-lib=static=res"); }
然后咱的主程序,通过 TCP 把链接发到 Linux,以及 Linux 端的服务,接收链接并打开浏览器,因为很简单很常规,所以这里就不列出来了。有兴趣的去源码仓库看就好了。
exe 编译好之后,扔到之前注册表文件里提及的地方就好。然后双击那个注册表文件将其导入。接下来在默认软件的设置里就能够找到我们的「Remote Browser」了(虽然不知道为什么没有显示指定的名字,而是 exe 文件名)。
另外,那个图标资源,也可以使用 Resource Hacker 把图标文件给弄到 exe 文件里边去。
最后一步,写个用户级的 systemd 服务,把负责在浏览器里打开链接的程序给跑起来~
仓库地址在此。喜欢的话,记得 star 哦~
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK