Sejak memasang "dark" theme, saya cenderung menjadi malas menulis. Untuk sementara, dark theme saya disable dulu yaa. Terima kasih (^_^) (bandithijo, 2024/09/15) ●

بسم الله الرحمن الرحيم

Pendahuluan

dmenu-websearch ini saya buat karena kemalasan saya dalam melakukan pencarian kata/frasa tertentu namun harus membuka website terkait. Tidak praktis. Saya ingin kata/frasa tersebut cukup saya inputkan dari menu di desktop kemudian browser akan menuntun saya ke halaman hasil pencarian di website terkait.

Fitur-fitur

dmenu-websearch ini memiliki fitur:

  1. mencari kata/frasa dari target website yang sudah didefinisikan di dalam file urlquery
  2. menambahkan target urlquery baru
  3. menghapus target urlquery yang ada (konfirmasi sebelum dihapus)

Snippet Code

dmenu-websearch script (Bash)

Simpan file dmenu-websearch di PATH direktori.

FILE~/.local/bin/dmenu-websearch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash

# Released under MIT License

# Copyright (C) 2021 Rizqi Nur Assyaufi <bandithijo@gmail.com>

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# Deps:
# - dmenu
# - jq

MENU="dmenu"

FILE="$HOME/.urlquery"

if [ ! -f $FILE ]; then
    cp urlquery.example $HOME/.urlquery
fi

FILEOPT=$(cat $FILE)

OPTIONS="
[
${FILEOPT}
[ \"Add:New:Query\", \"\" ],
[ \"Delete:Query\",  \"\" ]
]
"

OBJ_LENGTH=$(echo $OPTIONS | jq length)
OBJ_MENU=$(echo $OPTIONS | jq -r ".[][0]" | $MENU -i -p " WebSearch:")
OBJ_SELECTED=$(echo $OBJ_MENU | cut -d' ' -f1)
OBJ_KEYWORDS=$(echo $OBJ_MENU | cut -d' ' -f2- | tr " " "+")

add_query() {
    PREFIXMENU=$(echo "" | dmenu -p "Add prefix menu:" <&-)
    if [ ! -z $PREFIXMENU ]; then
        URLQUERY=$(echo "" | dmenu -p "Add url query:" <&-)
        echo "[ \"$PREFIXMENU\", \"$URLQUERY\" ]," >> $FILE
    fi
    exit
}

delete_query() {
    OBJ_MENU=$(echo $OPTIONS | jq -r ".[][0]" | head -n -2 | $MENU -i -p "Delete Menu:")
    OBJ_SELECTED=$OBJ_MENU
    if [ ! -z $OBJ_SELECTED ]; then
        CONFIRM_DELETE=$(echo -e "no\nyes" | dmenu -p "Do you want to delete this url?")
        if [ $CONFIRM_DELETE = "yes" ]; then
            sed -i "/$OBJ_SELECTED/d" $FILE
        fi
    fi
    exit
}

main() {
    for i in $(seq 0 $((OBJ_LENGTH - 1))); do
        if [ $OBJ_SELECTED = "Add:New:Query" ]; then
            add_query
        fi

        if [ $OBJ_SELECTED = "Delete:Query" ]; then
            delete_query
        fi

        if [ $OBJ_SELECTED = $(echo $OPTIONS | jq -r --arg i $i '.[($i|tonumber)][0]') ]; then
            URLQUERY=$(echo $OPTIONS | jq -r --arg i $i '.[($i|tonumber)][1]')
            KEYWORDS=$(echo $OBJ_KEYWORDS)
            xdg-open "${URLQUERY}${KEYWORDS}" 2> /dev/null
        fi
    done
}

main

urlquery (plaintext)

Simpan file .urlquery di HOME direktori.

FILE~/.urlquery
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[ "Arch:Manpage", "https://man.archlinux.org/search?q=" ],
[ "Arch:Wiki", "https://wiki.archlinux.org/index.php?search=" ],
[ "Arch:Packages", "https://archlinux.org/packages/?q=" ],
[ "Arch:Packages:AUR", "https://aur.archlinux.org/packages/?K=" ],
[ "DuckDuckGo:Search", "https://duckduckgo.com/?q=" ],
[ "GitHub:Search", "https://github.com/search?q=" ],
[ "Google:Search", "https://duckduckgo.com/?q=!g+" ],
[ "Google:Search:Image", "https://duckduckgo.com/?q=!gi+" ],
[ "Google:Translate", "https://translate.google.com/?sl=auto&tl=id&text=" ],
[ "Ruby:Gems", "https://rubygems.org/search?query=" ],
[ "Ruby:Toolbox", "https://www.ruby-toolbox.com/search?q=" ],
[ "Ruby:LibHunt", "https://ruby.libhunt.com/search?query=" ],
[ "Twitter:Search", "https://twitter.com/search?q=" ],
[ "Youtube:Search", "https://www.youtube.com/results?search_query=" ],
[ "Twitch:Search", "https://www.twitch.tv/search?term=" ],
[ "NPM:Search", "https://www.npmjs.com/search?q=" ],

Pesan Penulis

Penggunaan lebih lanjut saya serahkan pada imajinasi dan kreatifitas teman-teman.

Terima kasih sudah mampir yaa.

Referensi

  1. http://kb.mozillazine.org/Using_keyword_searches
    Diakses tanggal: 2022/08/14

  2. https://github.com/bandithijo/dmenu-websearch
    Diakses tanggal: 2022/08/14


Penulis

bandithijo

My journey kicks off from reading textbooks as a former Medical Student to digging bugs as a Software Engineer – a delightful rollercoaster of career twists. Embracing failure with the grace of a Cat avoiding water, I've seamlessly transitioned from Stethoscope to Keyboard. Armed with ability for learning and adapting faster than a Heart Beat, I'm on a mission to turn Code into a Product.

- Rizqi Nur Assyaufi

944e8edeccab170ecee65673676b75514b2f62ed