diff --git a/scripts/chromium-next-tab.sh b/scripts/chromium-next-tab.sh new file mode 100644 index 0000000..2644722 --- /dev/null +++ b/scripts/chromium-next-tab.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +# Chromium remote debugging port (default: 9222) +DEBUG_PORT=9222 + +# Get the list of open tabs in JSON format +tabs_json=$(curl -s http://localhost:$DEBUG_PORT/json) + +# Index of the currently focused tab (assumed to be the first) +focused_index=0 + +# Total number of tabs +total_tabs=$(echo "$tabs_json" | jq 'length') + +# Calculate next index (wrap around if at the end) +#next_index=$(( (focused_index + 1) % total_tabs )) +next_index=$(( (total_tabs - 1) % total_tabs )) # The last json object seems to be the next tab + +# Extract the ID of the next tab +next_tab_id=$(echo "$tabs_json" | jq -r ".[$next_index].id") +next_tab_title=$(echo "$tabs_json" | jq -r ".[$next_index].title") +next_tab_url=$(echo "$tabs_json" | jq -r ".[$next_index].url") + +# Occasionally there appears to be created a new empty and unusable tab. Not sure why. +# We tried to remove that tab when that happend, but was unsuccessfull. +# So now we just skip that tab when that happens: +if [ -z "$next_tab_url" ] || [ "$next_tab_url" = "about:blank" ]; then + echo "Tab [$next_index] [$next_tab_id] is empty, falling back to second-last" + next_index=$(( next_index - 1 )) + next_tab_id=$(echo "$tabs_json" | jq -r ".[$next_index].id") + next_tab_title=$(echo "$tabs_json" | jq -r ".[$next_index].title") + next_tab_url=$(echo "$tabs_json" | jq -r ".[$next_index].url") +fi + +# Activate the next tab +curl -s "http://localhost:$DEBUG_PORT/json/activate/$next_tab_id" > /dev/null +echo "Switched to tab [$next_index]: $next_tab_title ($next_tab_url | $next_tab_id) (total of $total_tabs)" \ No newline at end of file diff --git a/scripts/chromium-reload-tab.py b/scripts/chromium-reload-tab.py new file mode 100644 index 0000000..33f2370 --- /dev/null +++ b/scripts/chromium-reload-tab.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +# This is done in Python because doing websocket comms in bash is a pain in the ... +# Needs: "sudo apt install python3-websockets" to work +import asyncio +import websockets +import json +import subprocess + +# Get the WebSocket URL for the first tab +debug_info = subprocess.check_output(["curl", "-s", "http://localhost:9222/json"]) +tabs = json.loads(debug_info) +ws_url = tabs[0]["webSocketDebuggerUrl"] + +# Send reload command +async def reload_tab(): + async with websockets.connect(ws_url) as ws: + await ws.send(json.dumps({ + "id": 1, + "method": "Page.reload", + "params": {"ignoreCache": False} + })) + response = await ws.recv() + print("Reloaded tab:", response) + +asyncio.run(reload_tab()) diff --git a/scripts/runner.sh b/scripts/runner.sh index 1ed3a87..96e318d 100755 --- a/scripts/runner.sh +++ b/scripts/runner.sh @@ -11,10 +11,10 @@ chromium-browser \ --disable-smooth-scrolling \ --enable-accelerated-video-decode \ --enable-gpu-rasterization \ - --enable-low-end-device-mode \ --enable-oop-rasterization \ --force-device-scale-factor=1 \ --ignore-gpu-blocklist \ --kiosk \ --no-first-run \ - --noerrdialogs + --noerrdialogs \ + --remote-debugging-port=9222 diff --git a/scripts/setup.sh b/scripts/setup.sh index bfd44a4..0b53a98 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -35,7 +35,7 @@ else fi echo -e "${INFO}Installing dependencies...${RESET}" -apt install -y git jq wtype nodejs npm +apt install -y git jq wtype nodejs npm python3-websockets echo -e "${INFO}Cloning repository...${RESET}" git clone https://github.com/debloper/piosk.git "$PIOSK_DIR" diff --git a/scripts/switcher.sh b/scripts/switcher.sh index ba68e7b..1ed2d79 100755 --- a/scripts/switcher.sh +++ b/scripts/switcher.sh @@ -12,9 +12,9 @@ URLS=$(jq -r '.urls | length' /opt/piosk/config.json) # swich tabs each 10s, refresh tabs each 10th cycle & then reset for ((TURN=1; TURN<=$((10*URLS)); TURN++)) do if [ $TURN -le $((10*URLS)) ]; then - wtype -M ctrl -P Tab + /opt/piosk/scripts/chromium-next-tab.sh if [ $TURN -gt $((9*URLS)) ]; then - wtype -M ctrl r + /opt/piosk/scripts/chromium-reload-tab.py if [ $TURN -eq $((10*URLS)) ]; then (( TURN=0 )) fi