从 C# 在后台运行 Firefox
我使用 C# 运行 Firefox(默认浏览器),代码如下:
System.Diagnostics.Process.Start(browser.Document.Url.ToString());
我希望 Firefox 在后台运行,因为每次打开新选项卡时,Windows 都会专注于 Firefox,这很烦人。
我如何控制 Firefox 选项卡,在一段时间后关闭它们?
I run Firefox (default browser) from C# with the code:
System.Diagnostics.Process.Start(browser.Document.Url.ToString());
I want Firefox to run in the background, because every time is open a new tab, the Windows is focusing on the Firefox, and is annoying.
How can I control Firefox tabs, close them after a time ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 ProcessStartInfo 告诉它隐藏或最小化或其他方式运行。不确定如何以编程方式操作 FireFox,但我确信有一个 API。
You can use a ProcessStartInfo to tell it to run hidden or minimized or whatever. Not sure how to programmatically manipulate FireFox but I'm sure there's an API.
从技术上讲,您并不是在启动 Firefox,而是在执行一个 url。
我不确定 Windows 到底做了什么,但实际上,该 url 是在系统默认浏览器中打开的,无论是 IE、FF 还是其他甚至可能不支持选项卡的浏览器,因此查找并杀死 Firefox 并不是真正的解决方案如果该 url 在 Opera 中打开。
此外,如果调用实际上没有启动任何进程,
Process.Start
方法将返回 null,因此如果 Firefox 已经在运行并且只显示一个附加选项卡,您将得到一个null
作为调用的结果。因此,我非常确定从广义上讲(任何浏览器)这是不可能做到的,并且除非 Firefox 有某种用于客户端管理的 API,否则对于这种情况也是不可能的。
顺便说一句,在我的系统上(IE 是默认浏览器),
WindowStyle
属性未按预期工作,因为 IE 会弹出到前面。Technically, you are not starting Firefox, you are executing a url.
I'm not sure exactly what Windows does, but in effect, that url is opened in the system's default browser, be it IE, FF or some other thing that might not even support tabs, so finding and killing Firefox is not really a solution if the url is opened in Opera.
Moreover, the
Process.Start
method returns null if no process is actually started by the call, so if Firefox is already running and just displays an additional tab, you will get anull
as the result of the call.So, I'm pretty sure this is impossible to do in a broad sence (any browser), and, unless Firefox has some sort of API for client-side management, not possible for that scenario either.
BTW, on my system (IE is the default browser), the
WindowStyle
property is not working as expected, as IE pops up to the front.与其修剪选项卡,为什么不直接终止整个 Firefox 进程并定期重新启动它呢?
Rather than trimming the tabs, why not just kill the entire Firefox process and restart it periodically?
你将无法做到这一点。首先,我非常确定在后台运行 Firefox 不会阻止它在打开新选项卡时获得焦点。其次,很难以编程方式控制firefox。做你想做的事情的唯一方法是使用像 MozRepl 这样的插件。您还可以尝试使用 selenium 或您自己的 JavaScript 来控制浏览器行为。我需要能够在不使用 selenium 或 MozRepl 的情况下在 shell 脚本中打开和关闭选项卡,请查看我的问题 从 shell 脚本打开一个新选项卡在 Firefox 的特定实例中
You won't be able to do this. First of all, I'm pretty sure running Firefox in the background won't stop it gaining focus when a new tab is opened. Second, it is difficult to control firefox programmatically. The only way to do what you want is to use a plugin like MozRepl. You could also try using selenium or your own JavaScript to control the browser behaviour. I needed to be able to open and close tabs in a shell script without using selenium or MozRepl, check out my question From a shell script open a new tab in a specific instance of Firefox