LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2021, 01:53 PM   #1
Lennard37
LQ Newbie
 
Registered: Dec 2020
Posts: 28

Rep: Reputation: Disabled
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 ?

Last edited by Lennard37; 01-17-2021 at 05:37 PM.
 
Old 01-18-2021, 08:36 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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'
 
Old 01-18-2021, 08:50 AM   #3
Lennard37
LQ Newbie
 
Registered: Dec 2020
Posts: 28

Original Poster
Rep: Reputation: Disabled
[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.

Last edited by Lennard37; 01-18-2021 at 08:54 AM.
 
Old 01-18-2021, 09:04 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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'
 
Old 01-18-2021, 09:08 AM   #5
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by Lennard37 View Post
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.
 
Old 01-18-2021, 06:05 PM   #6
Lennard37
LQ Newbie
 
Registered: Dec 2020
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: 6 higher ed schools teach open source, Collaboration Summit remarks, and more news LXer Syndicated Linux News 0 05-07-2016 08:27 PM
LXer: T-Mobile urges FCC to “tread lightly” on video throttling and zero-rating LXer Syndicated Linux News 0 02-09-2016 07:33 AM
LXer: Tread Carefully in Shopping for Early Android Tablets LXer Syndicated Linux News 0 02-08-2011 12:31 AM
upgrading from stable to current improved/fixed my tread slackwaredanny Slackware 2 03-09-2010 09:36 PM
Testing tread rhicks Linux - Software 2 06-23-2002 09:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration