TopHome
<2024-03-14 Thu>techlinux

A minimal browser agnostic bookmarking system

What do we want? A simple web page bookmarking system with:

  1. Minimal - obviously.
  2. Multi-browser: I use a combination of Firefox and a little bit of Chrome. Will be nice to move between them without worrying.
  3. No extensions: most of these are black-box: who knows what is going on in the background. What is guarantee of safety of my data?
  4. In flow: should work within the browser context. Emacs is allowed - but even Emacs adds a bit of friction. This rules out solutions like buku, etc.

I briefly considered just installing Nyxt and being done with it, but that is not a good move on a work computer with internal websites. Best to go with the flow.

I hacked together something which works very well but is exceedingly inelegant. Here it is below:

#!/usr/bin/env bash

OP="$1"

# this is important! 
export YDOTOOL_SOCKET=/tmp/.ydotool_socket

if [[ "$OP" == "-s" ]]; then
    echo "Copying current url..."

    echo "Title for current page> "
    read title

    hyprctl dispatch focuscurrentorlast
    sleep 1

    # run F6 to select the url bar
    ydotool key -d 60 64:1 64:0
    # copy in the selected text using Ctrl-C
    ydotool key -d 30 29:1 46:1 46:0 29:0
    # run F6 again to defocus the url bar
    ydotool key -d 60 64:1 64:0
    msg=$(wl-paste -p)
    echo $title "|" $msg >> ~/.bookmarks
else
    url=$(cat ~/.bookmarks | fuzzel -d -w 80 | cut -d\| -f2)
    wl-copy $url
    if [[ "$url" != "" ]]; then
        # Run Ctrl-T to open a new tab
        ydotool key -d 30 29:1 20:1 29:0 20:0
        # Paste in using Ctrl-V
        ydotool key -d 30 29:1 47:1 29:0 47:0
        # Hit Enter to go
        ydotool key -d 60 28:1 28:0
    fi
fi

This depends on a number of things running:

  1. Meant for Wayland - use of the ydotool tool and the wl-clipboard package.
  2. Specifically for my window manager/compositor - hyprland.
  3. Needs the specified terminal emulator - alacritty to be available.
  4. Needs configuration of hyprland floating window rules to manage this popup terminal window.
  5. Needs the specifed dmenu like tool, fuzzel in my case.
  6. Needs keybindings in the Hyprland config.

1. Getting ydotool working

ydotool, the Wayland version of xdotool is a lot more work to setup. Step 4 onwards of the SO answer here helps.

The other thing that happens here is that ydotool key takes as input raw keycodes, not key names. You can get this info from /usr/include/linux/input-event-codes.h as described in the man page of ydotool.

2. Configuring Hyprland to support this

Add the following window rule into your Hyprland config:

windowrulev2 = float, class:floating 

We can now create windows (specifically alacritty in our case) that auto-float.

Now, we create binds in Hyprland as follows:

bind = SUPER, l, exec, ~/bin/bm
bind = SUPER SHIFT, l, exec, alacritty --class floating -e ~/bin/bm -s

We run the above script directly to pick up new windows. But, to save the current link as a bookmark, we take the circuitous route of a creating a terminal window which will run the script.

The script for its part uses hyprctl to move focus back to the browser window - which is where the initial focus has been assumed to start from. All this is needed due to the janky'ness of ydotool.

The whole setup works flawlessly, but like I said earlier, a massive kludge.