Skip to content

Commit

Permalink
#164 - run Chrome via separate process to avoid killing user's Chrome…
Browse files Browse the repository at this point in the history
… tabs

more details in issue
  • Loading branch information
kensoh committed May 28, 2018
1 parent ca0bf45 commit 743322f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
/src/tagui_service.done

# exclude local files for chrome integration
/src/chrome/tagui_user_profile
/src/tagui_chrome.in
/src/tagui_chrome.out
/src/tagui_chrome.log
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
***TagUI is now maintained by [AI Singapore (aisingapore.org)](https://www.aisingapore.org), a government-funded initiative to build local artificial intelligence capabilities. The intention is to add AI capabilities to TagUI while keeping it open-source and free to use. To get started, check out above hands-on tutorial, Prezi slides, or recorded video at FOSSASIA 2018.***

### FEATURES
***TagUI is a RPA / CLI tool for digital process automation***
***TagUI is a CLI tool for digital process automation (RPA)***
- automate Chrome visibly or headlessly
- visual automation of websites and desktop
- write in 20+ human languages & JavaScript
Expand Down
21 changes: 14 additions & 7 deletions src/tagui
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,6 @@ tagui_data_set_size=1
if [ -f "$1".csv ]; then
min_column=`awk -F',' '{print NF}' "$1".csv | sort -nu | head -n 1`
max_column=`awk -F',' '{print NF}' "$1".csv | sort -nu | tail -n 1`
# comment off sanity check for columns consistency as cells with , will trigger false-positive
# if [ "$min_column" != "$max_column" ]; then echo "ERROR - $1.csv has inconsistent # of columns" | tee -a "$1".log
if [ "$min_column" -lt 2 ]; then echo "ERROR - $1.csv has lesser than 2 columns" | tee -a "$1".log
else tagui_data_set_size="$(($min_column-1))"; fi; fi

Expand Down Expand Up @@ -273,17 +271,21 @@ headless_switch=""; if [ "$tagui_web_browser" == "headless" ]; then headless_swi
if [ "$tagui_data_set" -eq 1 ] || [ "$tagui_speed_mode" == false ]; then

# check for which operating system and launch chrome accordingly
chrome_started="$(uname -s)"; chrome_switches="--remote-debugging-port=9222 about:blank"
chrome_started="$(uname -s)"; chrome_switches="--user-data-dir=chrome/tagui_user_profile --remote-debugging-port=9222 about:blank"
if [ "$chrome_started" == "Darwin" ]; then
if ! [ -f "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" ]; then
echo "ERROR - cannot find Chrome at \"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome\""
echo "Chrome is at a non-standard location on your macOS, raise an issue on TagUI GitHub page"; exit 1; fi
pkill Google\ Chrome; /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome $chrome_switches $window_size $headless_switch > /dev/null 2>&1 &
chrome_process_id="$(ps | grep remote-debugging-port=9222 | grep tagui_user_profile | sed -e 's/^[ ]*//' | cut -d' ' -f 1 | sort -nur | head -n 1)"
if [ -n "$chrome_process_id" ]; then kill $chrome_process_id; fi
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome $chrome_switches $window_size $headless_switch > /dev/null 2>&1 &
else
if ! type "$chrome_command" > /dev/null; then
echo "ERROR - cannot find Chrome command \"$chrome_command\""
echo "update chrome_command setting in tagui/src/tagui and make sure symlink to command is created"; exit 1; fi
pkill $chrome_command; $chrome_command $chrome_switches $window_size $headless_switch > /dev/null 2>&1 &
chrome_process_id="$(ps | grep remote-debugging-port=9222 | grep tagui_user_profile | sed -e 's/^[ ]*//' | cut -d' ' -f 1 | sort -nur | head -n 1)"
if [ -n "$chrome_process_id" ]; then kill $chrome_process_id; fi
$chrome_command $chrome_switches $window_size $headless_switch > /dev/null 2>&1 &
fi

# wait until chrome is ready with websocket url for php thread
Expand All @@ -310,8 +312,13 @@ if [ -f "tagui.sikuli/tagui_sikuli.in" ]; then echo "finish" > tagui.sikuli/tagu
if [ -f "tagui_chrome.in" ]; then echo "finish" > tagui_chrome.in; fi

# kill chrome processes by checking which os the processes are started on
if [ "$chrome_started" == "Darwin" ] && [ "$tagui_speed_mode" == false ]; then pkill Google\ Chrome
else if [ -n "$chrome_started" ] && [ "$tagui_speed_mode" == false ]; then pkill $chrome_command; fi; fi
if [ "$chrome_started" == "Darwin" ] && [ "$tagui_speed_mode" == false ]; then
chrome_process_id="$(ps | grep remote-debugging-port=9222 | grep tagui_user_profile | sed -e 's/^[ ]*//' | cut -d' ' -f 1 | sort -nur | head -n 1)"
if [ -n "$chrome_process_id" ]; then kill $chrome_process_id; fi
else if [ -n "$chrome_started" ] && [ "$tagui_speed_mode" == false ]; then
chrome_process_id="$(ps | grep remote-debugging-port=9222 | grep tagui_user_profile | sed -e 's/^[ ]*//' | cut -d' ' -f 1 | sort -nur | head -n 1)"
if [ -n "$chrome_process_id" ]; then kill $chrome_process_id; fi
fi; fi

# end of big loop for managing multiple data sets in datatable
done
Expand Down
15 changes: 7 additions & 8 deletions src/tagui.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,6 @@ if not exist "%flow_file%.csv" goto no_datatable
for /f "tokens=* usebackq" %%c in (`gawk -F"," "{print NF}" "%flow_file%.csv" ^| sort -nu ^| head -n 1`) do set min_column=%%c
for /f "tokens=* usebackq" %%c in (`gawk -F"," "{print NF}" "%flow_file%.csv" ^| sort -nu ^| tail -n 1`) do set max_column=%%c

rem comment off sanity check for columns consistency as cells with , will trigger false-positive
rem if %min_column% neq %max_column% (
rem echo ERROR - %flow_file%.csv has inconsistent # of columns | tee -a "%flow_file%.log"
rem )
if %min_column% lss 2 (
echo ERROR - %flow_file%.csv has has lesser than 2 columns | tee -a "%flow_file%.log"
) else (
Expand Down Expand Up @@ -551,13 +547,14 @@ if exist "tagui_chrome.in" (

rem check for which operating system and launch chrome accordingly
set chrome_started=Windows
set chrome_switches=--remote-debugging-port=9222 about:blank
set chrome_switches=--user-data-dir=chrome\tagui_user_profile --remote-debugging-port=9222 about:blank
if not exist "%chrome_command%" (
echo ERROR - cannot find Chrome at "%chrome_command%"
echo update chrome_command setting in tagui\src\tagui.cmd to your chrome.exe
exit /b 1
)
taskkill /IM chrome.exe /T /F > nul 2>&1
for /f "tokens=* usebackq" %%p in (`wmic process where "caption like '%%chrome.exe%%' and commandline like '%%tagui_user_profile --remote-debugging-port=9222%%'" get processid 2^>nul ^| cut -d" " -f 1 ^| sort -nur ^| head -n 1`) do set chrome_process_id=%%p
if not "!chrome_process_id!"=="" taskkill /PID !chrome_process_id! /T /F > nul 2>&1
start "" "%chrome_command%" !chrome_switches! !window_size! !headless_switch!

:scan_ws_again
Expand Down Expand Up @@ -587,8 +584,10 @@ if exist "tagui.sikuli\tagui_sikuli.in" echo finish > tagui.sikuli\tagui_sikuli.
if exist "tagui_chrome.in" echo finish > tagui_chrome.in

rem kill chrome processes by checking which os the processes are started on
if not "!chrome_started!"=="" if %tagui_speed_mode%==false taskkill /IM chrome.exe /T /F > nul 2>&1

if not "!chrome_started!"=="" if %tagui_speed_mode%==false (
for /f "tokens=* usebackq" %%p in (`wmic process where "caption like '%%chrome.exe%%' and commandline like '%%tagui_user_profile --remote-debugging-port=9222%%'" get processid 2^>nul ^| cut -d" " -f 1 ^| sort -nur ^| head -n 1`) do set chrome_process_id=%%p
if not "!chrome_process_id!"=="" taskkill /PID !chrome_process_id! /T /F > nul 2>&1
)
rem end of big loop for managing multiple data sets in datatable
)
:break_for_loop
Expand Down

0 comments on commit 743322f

Please sign in to comment.