mirror of
https://github.com/LukeSmithxyz/voidrice.git
synced 2025-10-07 07:22:36 +02:00
Extremely Improved Bookmarks (Search + Strip URL + More)
The inspiration: (Luke Smith's Video): "Bookmarking for Unix Chads" Instructions & Justifications & Very Detailed Explanation for Everything can be found below. Short Summary - Allows users to manage and navigate bookmarks using dmenu. - The user can perform actions such as adding, deleting, and editing bookmarks. The script also handles searching within specific websites if the bookmarked URL has the word "search" in it. Such as the SearxNG Instance: "paulgo.io/search?q=". - One more interesting feature of it is that it modifies the order of bookmarks by their popularity (how frequently visited by you). - It can work with Dash and it has no bashisms. Execution Time: Instant Required Programs: dash (or bash) | jq | echo | grep | dmenu | notification daemon | browser | xclip (or "wl-paste" on Wayland) Instructions: These directories should exist: ~/.local/share/larbs/ 1. pacman -s jq (it's a very small program (I.E. 690kb) 2. Open a browser and highlight (xclip -o users) or copy (Wayland users) a website's URL (starting with "http") such as "https://github.com" 2. Run the script with a shortcut you created for the window manager that is used. 3. Write "@@" inside the terminal then enter (@@ can be changed of course). This opens the "Action Menu". 4. Select "Add a New Bookmark" option then enter. 5. The bookmark will be added inside the bookmarks menu. It can be edited or deleted later from the action menu that can be opened with "@@". 6. If a website that has the word "search" in it is added inside bookmarks such as a SearxNG instance "paulgo.io/search?q=" , its search function can be used. When selected, it will offer for a new prompt for the desired keywords. Justification: 1. The script uses jq for parsing and modifying JSON data, which is a lightweight and powerful command-line JSON processor. This choice allows for easy manipulation of the JSON file that stores bookmarks. 2. The use of the while loop with the dmenu interface ensures that the user can perform multiple actions (adding, deleting, or editing bookmarks) in a single session without needing to restart the script. 3. The script checks for an existing URLQUERY_FILE and initializes it with an empty JSON array "[]" if it doesn't exist or is empty. This ensures that the file is always in a valid state for the script to operate. 4. The script sorts bookmarks by popularity, allowing users to quickly access their most frequently visited bookmarks. This is achieved by incrementing a popularity value each time a bookmark is opened. 5. The script supports searching within specific websites by checking if the word "search" is in the URL (More words can be added). This feature provides a convenient way to search within bookmarked websites directly from the script. 6. The use of the dash shell (#!/bin/sh) as the interpreter provides a lightweight and fast shell environment for the script, improving performance. Since the script is efficient enough so this performance difference is not huge, so it can also be used with bash. Detailed Explanation 1. URLQUERY_FILE="~/.local/share/larbs/urlquery": Sets the path for the JSON file that will store the bookmarks. 2. The following block checks if the URLQUERY_FILE exists and if it's empty. If either condition is met, it initializes the file with an empty JSON array "[]". If there is no JSON array present, then no bookmark can be added. `if [ ! -f "$URLQUERY_FILE" ] || [ ! -s "$URLQUERY_FILE" ]; then echo "[]" > "$URLQUERY_FILE" fi` 3. ACTION_MENU='@@': Sets a special string for the action menu. 4. The read_bookmarks function reads the contents of the URLQUERY_FILE using jq. The write_bookmarks function writes the contents passed as an argument to a temporary file and then moves it to the URLQUERY_FILE. The update_bookmark_popularity function updates the popularity value of a bookmark, given its name. 5. The contains_search function checks if a given string contains the word "search". It returns 0 if it does, 1 otherwise. 6. bookmarks=$(read_bookmarks): Reads the bookmarks from the file and stores them in a variable. 7. touch "$URLQUERY_FILE": Ensures the URLQUERY_FILE exists by creating it if it doesn't. 8. The following while loop presents a menu to the user for choosing an action (Add, Delete, or Edit bookmark) or a bookmark to open. It continues until the user selects a bookmark to open or provides an empty input. `while true; do SELECTION=$(echo "$bookmarks" | jq -r '. | sort_by(.[2]) | reverse | .[] | .[0]' | rofi -dmenu -p "Bookmark") if [ -z "$SELECTION" ] || [ "$SELECTION" != "$ACTION_MENU" ]; then write_bookmarks "$bookmarks" break fi ACTION=$(echo "Add a New Bookmark\nDelete a Bookmark\nEdit a Bookmark" | rofi -dmenu -p "Action") #... done` 9. The case block inside the while loop performs the selected action (Add, Delete, or Edit) based on the user's input. 10. After the loop, the script checks if a valid bookmark is selected. If so, it updates the popularity value, writes the changes to the file, and opens the bookmark in Firefox or any browser. If the bookmark contains the word "search", it prompts the user for a search query before opening the URL in Firefox. 11. Creating and moving temporary files are for data integrity and safety. It ensures 0 file corruptions. Has no negative effect in terms of performance.
This commit is contained in:
parent
77fd62b9f3
commit
943220f884
95
.local/bin/chadmarked
Normal file
95
.local/bin/chadmarked
Normal file
@ -0,0 +1,95 @@
|
||||
#!/bin/sh
|
||||
|
||||
URLQUERY_FILE="~/.local/share/larbs/urlquery"
|
||||
ACTION_MENU='@@'
|
||||
|
||||
read_bookmarks() {
|
||||
if [ ! -f "$URLQUERY_FILE" ] || [ ! -s "$URLQUERY_FILE" ]; then
|
||||
echo "[]"
|
||||
else
|
||||
jq '.' "$URLQUERY_FILE"
|
||||
fi
|
||||
}
|
||||
|
||||
write_bookmarks() {
|
||||
echo "$1" > "${URLQUERY_FILE}.tmp" && mv "${URLQUERY_FILE}.tmp" "$URLQUERY_FILE"
|
||||
}
|
||||
|
||||
update_bookmark_popularity() {
|
||||
jq --arg name "$1" '(.[] | select(.[0] == $name))[2] += 1'
|
||||
}
|
||||
|
||||
contains_search() {
|
||||
case "$1" in
|
||||
*"search"*) return 0 ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
bookmarks=$(read_bookmarks)
|
||||
|
||||
while true; do
|
||||
SELECTION=$(echo "$bookmarks" | jq -r '. | sort_by(.[2]) | reverse | .[] | .[0]' | dmenu -l 10 -p "Bookmarks")
|
||||
|
||||
if [ -z "$SELECTION" ] || [ "$SELECTION" != "$ACTION_MENU" ]; then
|
||||
write_bookmarks "$bookmarks"
|
||||
break
|
||||
fi
|
||||
|
||||
ACTION=$(echo "Add a New Bookmark\nDelete a Bookmark\nEdit a Bookmark" | dmenu -l 3 -p "Action")
|
||||
|
||||
case "$ACTION" in
|
||||
"Add a New Bookmark")
|
||||
URL=$(xclip -o)
|
||||
if echo "$URL" | grep -q "^http"; then
|
||||
if ! echo "$bookmarks" | jq -e --arg url "$URL" '.[] | select(.[1] == $url)' > /dev/null; then
|
||||
NAME=$(dmenu -l 0 -p "Name")
|
||||
bookmarks=$(echo "$bookmarks" | jq --arg name "$NAME" --arg url "$URL" '. |= .+ [[ $name, $url, 0 ]]')
|
||||
notify-send "'$NAME' is bookmarked."
|
||||
else
|
||||
notify-send "The URL is already in the list."
|
||||
fi
|
||||
else
|
||||
notify-send "The clipboard content is not a valid URL."
|
||||
fi
|
||||
;;
|
||||
"Delete a Bookmark")
|
||||
NAME=$(echo "$bookmarks" | jq -r '.[] | .[0]' | dmenu -p "Delete")
|
||||
if [ -n "$NAME" ]; then
|
||||
bookmarks=$(echo "$bookmarks" | jq --arg name "$NAME" 'del(.[] | select(.[0] == $name))')
|
||||
notify-send "'$NAME' is deleted."
|
||||
fi
|
||||
;;
|
||||
"Edit a Bookmark")
|
||||
NAME=$(echo "$bookmarks" | jq -r '.[] | .[0]' | dmenu -p "Edit")
|
||||
FIELD=$(echo "name\nURL" | dmenu -l 2 -p "Edit")
|
||||
|
||||
if [ -n "$NAME" ] && [ -n "$FIELD" ]; then
|
||||
NEW_VALUE=$(dmenu -l 0 -p "New $FIELD")
|
||||
if [ "$FIELD" = "name" ]; then
|
||||
bookmarks=$(echo "$bookmarks" | jq --arg name "$NAME" --arg new_name "$NEW_VALUE" '(.[] | select(.[0] == $name))[0] = $new_name')
|
||||
else
|
||||
bookmarks=$(echo "$bookmarks" | jq --arg name "$NAME" --arg new_url "$NEW_VALUE" '(.[] | select(.[0] == $name))[1] = $new_url')
|
||||
fi
|
||||
notify-send "'$NAME' is updated."
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ -n "$SELECTION" ] && [ "$SELECTION" != "$ACTION_MENU" ]; then
|
||||
URL=$(echo "$bookmarks" | jq -r --arg name "$SELECTION" '.[] | select(.[0] == $name) | .[1]')
|
||||
|
||||
if [ -z "$URL" ] || [ "$URL" = "null" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if contains_search "$URL"; then
|
||||
QUERY=$(dmenu -l 0 -p "Search")
|
||||
URL="${URL}${QUERY}"
|
||||
fi
|
||||
|
||||
bookmarks=$(echo "$bookmarks" | update_bookmark_popularity "$SELECTION")
|
||||
write_bookmarks "$bookmarks"
|
||||
$BROWSER "$URL"
|
||||
fi
|
||||
Loading…
x
Reference in New Issue
Block a user