AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 853891
Accepted
a3k
a3k
Asked: 2016-11-27 08:15:14 +0800 CST2016-11-27 08:15:14 +0800 CST 2016-11-27 08:15:14 +0800 CST

如何让 Firefox 像铬一样在后台运行?

  • 772

Chromium 中的一个已知功能是使其在后台运行的选项,这使得打开 .

是否可以对 firefox(和其他应用程序)做同样的事情?

firefox
  • 2 2 个回答
  • 6601 Views

2 个回答

  • Voted
  1. Best Answer
    Jacob Vlijm
    2016-11-28T02:29:53+08:002016-11-28T02:29:53+08:00

    在后台运行应用程序

    下面的解决方案将允许您在后台运行 firefox(或任何其他应用程序),这意味着:没有可见窗口。该应用程序也不会在 Dash 中显示为正在运行的应用程序:

    在此处输入图像描述

    在此处输入图像描述

    但是,如果您选择Toggle Firefox,应用程序将立即弹出:

    在此处输入图像描述

    这个怎么运作

    1. 如果面板图标(指示器)启动,它会启动一个新firefox窗口,但会立即将其(包括可能存在的firefox窗口)从地球表面隐藏起来,使用xdotool:

      xdotool windowunmap <window_id>
      

      这不仅会隐藏窗口,还会隐藏firefox正在运行的事实,因为统一启动器作用于可见的现有窗口。

    2. 指示器将所有未映射窗口的 id 存储在 中~/.config/hidden_windows,以便在您下次Toggle Firefox从菜单中选择时映射。

    剧本

    #!/usr/bin/env python3
    import subprocess
    import os
    import signal
    import gi
    gi.require_version('Gtk', '3.0')
    gi.require_version('AppIndicator3', '0.1')
    from gi.repository import Gtk, AppIndicator3
    import time
    
    app = "firefox"
    winsdir = os.path.join(os.environ["HOME"], ".config/hidden_windows")
    
    try:
        os.mkdir(winsdir)
    except FileExistsError:
        pass
    
    def checkruns(app):
        try:
            return subprocess.check_output(["pgrep", app]).decode("utf-8").strip()
        except subprocess.CalledProcessError:
            for w in os.listdir(winsdir):
                if w.startswith(app):
                    os.remove(os.path.join(winsdir, w))
    
    def get_currentwins(pid):
        wins = subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8").splitlines()
        return [l.split()[0] for l in wins if pid in l]
    
    def hide_currentwins(matches):
        # open(os.path.join(winsdir, "windowlist"), "wt").write("\n".join(matches))
        for w in matches:
            open(os.path.join(winsdir, app+"_"+w), "wt")
            subprocess.Popen(["xdotool", "windowunmap", w])
    
    def run_app(app):
        subprocess.Popen(app)
        while True:
            time.sleep(1)
            pid = checkruns(app)
            matches = get_currentwins(pid) if pid else None
            if matches:
                hide_currentwins(matches)
                break
    
    def restore_wins():
        for w in [w for w in os.listdir(winsdir) if w.startswith(app)]:
            wid = w.split("_")[-1]
            subprocess.Popen(["xdotool", "windowmap", wid])
            os.remove(os.path.join(winsdir, w))
    
    def toggle_app(*args):
        pid = checkruns(app)
        if pid:
            matches = get_currentwins(pid)
            if matches:
                hide_currentwins(matches)
            else:
                restore_wins()
        else:
            subprocess.Popen(app)
    
    run_app(app)
    
    class Indicator():
        def __init__(self):
            self.app = 'toggle_app'
            self.indicator = AppIndicator3.Indicator.new(
                self.app, app,
                AppIndicator3.IndicatorCategory.OTHER)
            self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)       
            self.indicator.set_menu(self.create_menu())
    
        def create_menu(self):
            self.menu = Gtk.Menu()
            item_toggle = Gtk.MenuItem('Toggle '+app)
            item_toggle.connect("activate", toggle_app)
            self.menu.append(item_toggle)
            sep1 = Gtk.SeparatorMenuItem()
            self.menu.append(sep1)
            item_quit = Gtk.MenuItem('Quit')
            item_quit.connect('activate', self.stop)
            self.menu.append(item_quit)
            self.menu.show_all()
            return self.menu
    
        def stop(self, source):
            Gtk.main_quit()
    
    Indicator()
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    Gtk.main()
    

    如何使用

    1. 该脚本需要wmctrl同时xdotool

      sudo apt-get install wmctrl xdotool
      
    2. 将脚本复制到一个空文件中,另存为firefox_bg.py

    3. 通过以下命令测试_运行脚本:

      python3 /path/to/firefox_bg.py
      
    4. 如果一切正常,请将其添加到启动应用程序:Dash > Startup Applications > Add。添加命令:

      /bin/bash -c "sleep 10 && python3 /path/to/firefox_bg.py"
      

      或者,将下面的代码复制到一个空文件中,将其另存为firefox_bgrunner.desktopin ~/usr/share/applications,注销并重新登录。

      [Desktop Entry]
      Type=Application
      Exec=python3 /path/to/firefox_bg.py
      Name=Firefox Webbrowser Background Runner
      Icon=firefox
      StartupWMClasss=nonsense
      

      *最后一行,StartupWMClasss=nonsense是为了确保Firefox windows will appear under their own icon, not the one of the indicator.

      无需提及您必须编辑该Exec=行以反映您存储的真实(绝对)路径firefox_bg.py

      然后您将获得 Dash 提供的面板运行器:

      在此处输入图像描述

    其他应用?

    gnome-terminal我用and测试了相同的程序Thunderbird(后者通常不是最快的启动),它运行良好:

    在此处输入图像描述

    要与其他应用程序一起使用,只需编辑以下行:

    app = "firefox"
    

    但是请注意,某些应用程序似乎会检查他们创建窗口的尝试是否成功,如果第一个窗口未映射,则创建第二个窗口。这发生在我身上Inkscape。

    该脚本甚至可以完美地使用,但需要进行少量编辑。如果有人可能需要使用它Inkscape,请发表评论。

    • 6
  2. Fmstrat
    2019-06-27T07:56:36+08:002019-06-27T07:56:36+08:00

    我想为带有 GNOME 的 Ubuntu 18-19+ 提出一个 2019 年的解决方案,它稍微简单一点(IMO),并且为了简单起见使用 bash 而不是 python。我这样做是为了让 Firefox 在我登录时会询问我的主密码,但除非我去查看密码,否则不会再次询问。每次启动 Firefox 时,我都厌倦了它。

    首先安装依赖项:

    sudo apt-get update && apt-get install -y wmctrl xdotool
    

    然后把这个脚本放在某个地方,说/data/system/bin/firefox-background:

    firefox "about:blank" &
    WAITFOR="Mozilla Firefox"
    
    APP=$(wmctrl -lp |grep "${WAITFOR}" |awk '{print $1}')
    while [ -z "${APP}" ]; do
        sleep 1
        APP=$(wmctrl -lp |grep "${WAITFOR}" |awk '{print $1}')
    done
    xdotool windowunmap ${APP}
    

    和:

    chmod +x /data/system/bin/firefox-background
    

    现在您可以运行此脚本,例如从终端窗口或从 GNOME 启动中使用以下文件.config/autostart/FirefoxBackground.desktop:

    [Desktop Entry]
    Name=Firefox Background
    Exec=/data/system/bin/firefox-background
    Terminal=false
    Icon=firefox
    Type=Application
    StartupNotify=false
    X-GNOME-Autostart-enabled=true
    StartupWMClasss=nonsense
    

    之后,您将获得一次主密码弹出窗口,除非您打算访问安全信息,否则再也不会出现。

    • 0

相关问题

  • Flash 在 Firefox 中无法正常工作。不会响应某些点击[关闭]

  • Firefox 鼠标中键滚动

  • 如何解决 Firefox 中的字体问题?

  • 使用突触或通过 Firefox 的插件菜单安装 adblock?

  • Firefox 的 Ubuntu Firefox 修改扩展有什么作用?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve