LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Any remarks for doing bash script more tread save ? (https://www.linuxquestions.org/questions/programming-9/any-remarks-for-doing-bash-script-more-tread-save-4175688760/)

Lennard37 01-17-2021 01:53 PM

Any remarks for doing bash script more tread save ?
 
The follow bash script mark adressbar from firefox, copy the url, echo the url, extract the domain name and echo the domain name.

The script working some rounds and after some time like 10 minutes or 5 hours, xdotool send by self, additional and without to stop by self, additional to the running script, by self by script used keys.

bash script:
Quote:

#!/bin/bash

while true; do

# mark the adressbar from browser and copy the url
xdotool sleep 0.12 search --onlyvisible --classname Navigator windowactivate --sync key --clearmodifiers --delay 12 F6 Ctrl+c F6 Escape; echo
sleep 1
echo
echo

# copy url from clipboard to var
clipboard_url=$( xsel -ob )
echo URL":" "$clipboard_url"
sleep 1
echo
echo

# clear clipboard
xsel -bc && xsel -c
sleep 1
echo
echo

# extract domain from url
url=$clipboard_url
domain=$(echo $url | sed -e 's/[^/]*\/\/\([^@]*@\)\?\([^:/]*\).*/\2/')
echo Domain":" $domain
sleep 1
echo
echo

done

Error": the keyboard keys which send by script after some times by self, sometimes without to stopp to send:
Quote:

F6
Ctrl+c
Ctrl+l

Ctrl, sometimes the Ctrl key remains press like with missing keyup
Any idea how what can be do better and more threadsafe ?

shruggy 01-18-2021 08:36 AM

I'd approach this from a different angle. To get URLs from the address bar, you can evaluate the contents of recovery.jsonlz4 as described here.

You'll have to unpack it first: either with lz4jsoncat (package lz4json is available in recent Debian-based distros, but it's easy to compile from source) or with a simple Python script (needs to be slightly adjusted to work with recent versions of lz4):
Code:

sed -i '/^#/!s/lz4/&.block/' mozlz4a.py
Then you can do something like
Code:

python3 mozlz4a.py -d \
  ~/.mozilla/firefox/*.default*/sessionstore-backups/recovery.jsonlz4 \
  /dev/stdout|
  jq '.windows[].tabs[].entries[]|select(.loadReplace)|.url'


Lennard37 01-18-2021 08:50 AM

[QUOTE=shruggy;6209117]I'd approach this from a different angle. To get URLs from the address bar, you can evaluate the contents of recovery.jsonlz4 as described here.

Firefox dont offer "recovery.js" since some time. I cant do it on this way. It tryed to do by some python scripts from the new location and filename. A I didnt be successfull on this. So I try to do it by some shortcuts and bash.

Quote:

import json
import lz4.block
import pathlib
from time import time

# Set up path and regex for files
path = pathlib.Path.home().joinpath('.mozilla/firefox')
# files = path.glob('*default*release*/sessionstore-backups/recovery.jsonlz4')
files = path.glob('5ov6afhq.default-release-1/sessionstore-backups/recovery'+'.jsonlz4')

for f in files:
# decompress if necessary
b = f.read_bytes()
if b[:8] == b'mozLz40\0':
b = lz4.block.decompress(b[8:])

# load as json
j = json.loads(b)
if 'windows' in j.keys():
for w in j['windows']:

# Variables for keeping track of most recent tab
most_recent_tab_index = ''
min_time = 1000

# run through tabs
for t in w['tabs']:
# Firefox does not 0-index
i = t['index'] - 1

# Convert time to seconds
access_time = int((int(time()*1000) - t['lastAccessed'])/600)

if access_time < min_time:
most_recent_tab_index = t['entries'][i]['url']

print("MOST RECENT TAB: ", most_recent_tab_index)
Source: https://askubuntu.com/questions/9296...a-command-line

By the above one, I get not the URL or Domain from current page. I get only the URL from my startpage.

shruggy 01-18-2021 09:04 AM

The Python script you posted works for me. Obviously, you'll have to adjust the files = line:
Code:

files = path.glob('*.default*/sessionstore-backups/recovery.jsonlz4'

shruggy 01-18-2021 09:08 AM

Quote:

Originally Posted by Lennard37 (Post 6209127)
By the above one, I get not the URL or Domain from current page. I get only the URL from my startpage.

Ah, now I understand. Then you should examine the contents of recovery.jsonlz4 and find out how to get the active tab.

Lennard37 01-18-2021 06:05 PM

Quote:

Originally Posted by shruggy (Post 6209131)
The Python script you posted works for me. Obviously, you'll have to adjust the files = line:
Code:

files = path.glob('*.default*/sessionstore-backups/recovery.jsonlz4'

It canbe I have to figure that out again. On this time I am getting a error.


All times are GMT -5. The time now is 10:01 PM.