Reputation: 1109
I wrote the following script to document the webpages I visit. When run, the text content of the active tab will be logged and stored in a directory. What I would like to be able to do is have a new directory and file created on the fly every time the page changes; whether another tab is activated or another url is loaded in the same tab. As it is right now, the script will log all content but it will log all content in the directory for the web page that was open when the script was started. So if I start the script while this page is active, any other page I visit will be logged in the same file and directory directly below the content logged from this page.
#!/bin/bash
getChrome() {
osascript -e \
'tell application "Google_Chrome" to tell the active tab of window 1 to execute javascript "document.body.innerText"'
}
url="$(osascript -e 'tell application "Google_Chrome" to set the_URL to the URL of the active tab of window 1')"
site="$(echo $url | cut -d/ -f3)"
# removes . and replaces with _
if [[ $(echo "$site") =~ '.' ]]; then
site="$(echo "${site//./_}")"
fi
ds="$(date "+%m-%d-%y")"
dir="$HOME/Desktop/netlogs/$site/$ds"
if [ ! -d "${dir}" ]; then mkdir -p "${dir}"; fi
doc="${site}_LOG.txt"
file="${dir}/${doc}"
printf "\nBEGIN\n\n" | tee -a $file
while true
do
getChrome | while read lines
do
echo $lines
# This if statement doesn't work.
# Included here to show intent
if [[ $url != $url ]]; then
break 1
fi
done
done | awk '!seen[$0]++ { print; fflush() }' | tee -a $file
In trying to figure out a way of doing this I wrote this Applescript to get some audio feedback on what is happening when I change tabs:
tell application "Google_Chrome" to tell window 1
tell the active tab
set the_url to the URL
repeat
if the URL is not the_url then
set the_url to the URL
say "nope"
else
say "yep"
end if
end repeat
end tell
end tell
Aside from being pretty annoying, it does let me know that a change of url is recognized. But I can't figure out how to take this same idea and use it to make a new directory and file when the url changes. I'm not looking for an AppleScript solution necessarily. In fact I prefer to avoid AppleScript as much as possible as it usually brings headaches and eventual rage quitting. But when it works it works well and I'd be happy with any method that does that.
Upvotes: 1
Views: 105
Reputation: 1109
#!/bin/bash
getSAFARI() {
osascript -e \
'tell application "Safari" to tell current tab of window 1 to do javascript "document.body.innerText"' 2>&1
}
getCHROME() {
osascript -e \
'tell application "Google_Chrome" to tell active tab of window 1 to execute javascript "document.body.innerText"' 2>&1
}
SURF() {
if [[ "$(echo $current_url)" != "$(echo $url)" ]]; then
echo -e "\nEND\n\n" | tee -a $file 2>&1
break 1
elif [[ "$(echo $current_url)" = https://example.com/*/ ]]; then
regex='[[:alnum:]]'
else
regex='[\p{L}]'
fi
get$BROWSER | while read lines; do echo $lines | grep -E $regex; done < <(get$BROWSER)
}
SET_VARIABLES() {
ds="$(date "+%m_%d_%y")"
site="$(echo $url | cut -d/ -f3)"
dir="$HOME/Desktop/netlogs/$ds/$site"
doc="${site}_LOG.txt"
file="${dir}/${doc}"
if [ ! -d "${dir}" ]; then mkdir -p "${dir}"; fi
echo '-----------------------------' | tee -a $file 2>&1
date | tee -a $file 2>&1
echo -ne "$url\n" | tee -a $file 2>&1
echo '-----------------------------' | tee -a $file 2>&1
printf "BEGIN\n\n" | tee -a $file 2>&1
}
echo "Choose your browser"
echo "1) Safari"
echo "2) Chrome"
echo
read -sn1 choice
while true
do
case $choice in
[1])
BROWSER="SAFARI"
while true
do
url="$(osascript -e 'tell app "Safari" to tell the current tab of window 1 to get the url as text' 2>&1 | grep -v 'execution error')"
if [[ $url = '' ]]; then echo "Now exiting" && exit; fi
SET_VARIABLES
while true
do
current_url="$(osascript -e 'tell app "Safari" to tell the current tab of window 1 to get the url as text' 2>&1 | grep -v 'execution error')"
SURF
if [[ $current_url = '' ]]; then echo "Now exiting" && exit; fi
done | awk '!seen[$0]++ { print; fflush() }' | tee -a $file
done
break
;;
[2])
BROWSER="CHROME"
while true
do
url="$(osascript -e 'tell app "Google_Chrome" to tell the active tab of window 1 to get the url as text' 2>&1 | grep -v 'error')"
if [[ $url = '' ]]; then echo "Now exiting" && exit; fi
SET_VARIABLES
while true
do
current_url="$(osascript -e 'tell app "Google_Chrome" to tell the active tab of window 1 to get the url as text' 2>&1 | grep -v 'error')"
SURF
if [[ $current_url = '' ]]; then echo "Now exiting" && exit; fi
done | awk '!seen[$0]++ { print; fflush() }' | tee -a $file
done
break
;;
*)
echo 'Invalid Selection'
exit
;;
esac
done
Upvotes: 1