├── .github └── FUNDING.yml ├── .gitignore ├── README.md ├── browsermediacontrol └── screenshot.png /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: ['https://www.buymeacoffee.com/HAliPunjabi'] 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | tags -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # polybar-browsermediacontrol 2 | 3 | A Browser Media Control module for [Polybar](https://github.com/jaagr/polybar) 4 | 5 | ![screenshot](screenshot.png) 6 | Demo with [Material Polybar](https://github.com/adi1090x/polybar-themes) 7 | 8 | ## Dependencies 9 | * [python3](https://www.python.org) 10 | * [pydbus](https://github.com/LEW21/pydbus) 11 | * [Plasma Browser Integration](https://community.kde.org/Plasma/Browser_Integration) 12 | * Iosevka Nerd Font 13 | 14 | ## Usage 15 | Place the given script in some folder, and use it in your polybar `config` as 16 | ``` 17 | [module/browsermediacontrol] 18 | type = custom/script 19 | exec = /path/to/files/browsermediacontrol 20 | scroll-up = /path/to/files/browsermediacontrol --volume 1 21 | scroll-down = /path/to/files/browsermediacontrol --volume -1 22 | interval = 0.1 23 | ```` 24 | *Note: You can change the integer argument in scroll-up/down to increase the speed of increase/decrease* 25 | ## Features: 26 | * Customizable Title Truncate 27 | * Volume Control on Scroll 28 | 29 | ## Customization 30 | You can change the variables in [`browsermediacontrol`](browsermediacontrol) to customize the icons shown in [polybar](https://github.com/jaagr/polybar), text overflow length, etc. 31 | 32 | -------------------------------------------------------------------------------- /browsermediacontrol: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | from pydbus import SessionBus 3 | from gi.repository import GLib 4 | import sys 5 | import os 6 | import argparse 7 | import re 8 | 9 | ICON_PLAY = "\uf144" 10 | ICON_PAUSE = "\uf28b" 11 | ICON_NEXT = "\uf152" 12 | ICON_PREV = "\uf191" 13 | ICON = "" 14 | PATH=os.path.realpath(__file__) 15 | TITLE_LENGTH = 25 16 | MEDIA_ICONS = { 17 | 'apple': '\uf302', 18 | 'spotify': '\uf1bc', 19 | 'youtube': '\uf16a' 20 | } 21 | PATTERN = '(' + '|'.join(MEDIA_ICONS.keys()) + ')' 22 | MEDIA_ICONS_REGEX = re.compile(PATTERN, re.MULTILINE) 23 | 24 | parser = argparse.ArgumentParser() 25 | parser.add_argument('--limit', type=int) 26 | parser.add_argument('--volume', type=int) 27 | parser.add_argument('--playpause', action="store_true") 28 | parser.add_argument('--next', action="store_true") 29 | parser.add_argument('--prev', action="store_true") 30 | args= parser.parse_args() 31 | 32 | bus = SessionBus() 33 | try: 34 | Player = bus.get( 35 | "org.kde.plasma.browser_integration", 36 | "/org/mpris/MediaPlayer2" 37 | ) 38 | except GLib.Error: 39 | exit() 40 | 41 | def action(command, text): 42 | return "%{A1:" + PATH + " --" + command + ":}" + text + "%{A}" 43 | 44 | def truncate(text, max_len): 45 | return text[:max_len] + "..." if len(text) > max_len and max_len > 0 else text 46 | 47 | def get_media_icon(url): 48 | match = MEDIA_ICONS_REGEX.findall(url) 49 | if match: 50 | if len(match) == 1: 51 | return MEDIA_ICONS[match[0]] 52 | return None 53 | 54 | if Player.PlaybackStatus != "Stopped": 55 | if args.volume != None: 56 | vol = Player.Volume 57 | Player.Volume = vol + (args.volume * 0.1) 58 | exit() 59 | if args.playpause: 60 | Player.PlayPause() 61 | exit() 62 | if Player.PlaybackStatus=="Playing": 63 | ICON = ICON_PAUSE 64 | elif Player.PlaybackStatus=="Paused": 65 | ICON = ICON_PLAY 66 | if args.next: 67 | Player.Next() 68 | elif args.prev: 69 | Player.Previous() 70 | 71 | if args.limit: 72 | TITLE_LENGTH = args.limit 73 | 74 | title = Player.Metadata["xesam:title"] 75 | media_icon = get_media_icon(Player.Metadata["xesam:url"].lower()) 76 | output = action("prev", ICON_PREV) + " " + action("playpause", ICON) + " " + action("next", ICON_NEXT) + " | " + ( f"{media_icon} " if media_icon else "" ) + truncate(title, TITLE_LENGTH) 77 | print(output) 78 | else: 79 | print("") 80 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haideralipunjabi/polybar-browsermediacontrol/28e4b795dbb4504dbd6897cc7ed952d6cf84c1bb/screenshot.png --------------------------------------------------------------------------------