0

I have to lunch multiple browsers (thousands), but don't want to open a new window every time a browser is launched; instead, I need to open the webpage, process the content and go to the next.

When running Chrome (it doesn't have to be Chrome) from the terminal:

$ google-chrome URL

the webpage is opened in a new window of the actual browser:

Created new window in existing browser session.

Is there any way to run it on the actual windows, and not open a new one?

Alternatively: Is there a way to kill the currently created browser session before launching the next one? How do I get the pid of that new session just created if the browser was initially closed?

2 Answers 2

1

effectively, what you want is opening a new url in a window without closing the current window manually (if I understand well), nor having to close tabs manually.

Basic solution

The solution could be to run a command that combines two things:

  1. open a new window from the command line with the url as argument
  2. automatically close the current window

This way the pid of the browser will not change, and you would not have to manually close tabs and/or windows.

The small script below does exactly that, using firefox. The reason to use firefox is that (e.g.) chrome has multiple pids by definition, making the process more complicated.

installing wmctrl
The script uses wmctrl to find and close the current firefox windows, you might have to install it first:

sudo apt-get install wmctrl

The script

#!/usr/bin/env python3

import subprocess
import socket
import sys

url =sys.argv[1]

def get_info(command):
    return subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8").strip()

def run_command(command):
    subprocess.call(["/bin/bash", "-c", command])

currpid = get_info("pidof firefox")
browserwindow = [l.split()[0] for l in get_info("wmctrl -l -p").splitlines() if currpid in l]
run_command("firefox -new-window "+url)
for i in browserwindow:
    run_command("wmctrl -ic "+i)

How to use

Copy the script into an empty file, save it as firejump.py. Open a firefox window (any address, just a window) and run the script by the command:

python3 /path/to/firejump.py <new_url>

Disable warning to close the current window

For the procedure to run without the annoying firefox warning that you are closing the current tabs or windows: disable the warnings by unticking it in the preferences:

enter image description here

More advanced solution

If you really need to jump over a list of thousands of url's, it might be useful to extend the script a little (see the script below), and let it automatically jump to the next url from a list, that you created in a text file, and run the script with a key combination to jump to the next url.

How to use

  • Copy the script below into an empty texfile, save it as jump_list.py
  • create in the same folder a text file, named (exactly): urls.txt
  • fill the file with your urls, one per line:

    www.something.com
    www.another_thing.com
    www.yet_another_thing.nl
    www.itshardtostop.be
    

and so on

Open a firefox window (any address, just a window).
Now when you run the script, firefox opens the first url.
Run it again and it will jump to the next url and so on, until the last url of the list has passed.

Explanation

The script reads the lines in the file urls.txt by index. It keeps track of the (last used) index in a file that the script creates in the script's own directory. Every time the script runs, the number is increased by one until the last line is reached.

The script

#!/usr/bin/env python3

import subprocess
import os

curr_path = os.path.dirname(os.path.abspath(__file__))

def get_info(command):
    return subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8").strip()

def run_command(command):
    subprocess.call(["/bin/bash", "-c", command])

def write_totrack(file, next_url):
    with open(file, "wt") as wrt:
        wrt.write(next_url)

def read_file(file):
    with open(file) as src:
        return src.read()

keep_track = curr_path+"/"+"next_visit.txt"
url_list = read_file(curr_path+"/"+"urls.txt").splitlines()

if not os.path.exists(keep_track):
    next_url = "1"; index = 0
    write_totrack(keep_track, next_url)
else:
    index = int(read_file(keep_track))
    next_url = index+1
    write_totrack(keep_track, str(next_url))
if index < len(url_list):
    curr_url = url_list[index]
    currpid = get_info("pidof firefox")
    browserwindow = [l.split()[0] for l in get_info("wmctrl -l -p").splitlines() if currpid in l]
    run_command("firefox -new-window "+curr_url)
    for i in browserwindow:
        run_command("wmctrl -ic "+i)   
else:
    pass

Run the script by:

python3 /path/to/jump_list.py

Add it to $PATH

More elegant would be to save the script to ~/bin, make it executable, remove the language extension and run it by the command:

jump_list

Add to Shortcut Keys

To really make it comfortable, add it to a shortcut combination: System Settings > "Shortcut Keys" > "Custom Shortcuts"

0

I have an idea for the alternative solution: You can check with

ps -ef | grep chrome

the process number of the running chrome session. With

kill [process number]

you can kill the process. Hope it works for you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .