commit b8f0472e545929f9435f38af57f523f1b61bdf12 Author: Markus Birth Date: Sun Dec 19 19:56:17 2021 +0100 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..4971b1c --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +Hammerspoon Scripts +=================== + +This is my [Hammerspoon](http://www.hammerspoon.org/) configuration. + + +Spoons +------ + +h3. BbcSounds + +A HS implementation of [Richard Taylor's BBC Sounds status bar app](https://github.com/moomerman/Sounds) +basically similar to what he explained [on his website](https://www.richardtaylor.dev/articles/building-bbc-sounds-app-for-macos/). + diff --git a/Spoons/BbcSounds.spoon/icon_16.png b/Spoons/BbcSounds.spoon/icon_16.png new file mode 100644 index 0000000..7d0abae Binary files /dev/null and b/Spoons/BbcSounds.spoon/icon_16.png differ diff --git a/Spoons/BbcSounds.spoon/icon_32.png b/Spoons/BbcSounds.spoon/icon_32.png new file mode 100644 index 0000000..c6b2cc1 Binary files /dev/null and b/Spoons/BbcSounds.spoon/icon_32.png differ diff --git a/Spoons/BbcSounds.spoon/init.lua b/Spoons/BbcSounds.spoon/init.lua new file mode 100644 index 0000000..1da8191 --- /dev/null +++ b/Spoons/BbcSounds.spoon/init.lua @@ -0,0 +1,99 @@ +--- === BBC Sounds === +--- +--- Implementation of https://github.com/moomerman/Sounds in Hammerspoon. + +local obj = {} +obj.__index = obj + + +obj.name = "BBC Sounds" +obj.version = "0.1" +obj.author = "Markus Birth " + +function obj:init() + self.webView = hs.webview.new({x=0, y=0, w=900, h=716}) + self.eventTap = nil + self.log = hs.logger.new(self.name, "verbose") +end + +function obj:start() + self.log.v('Start') + self.webView:allowNewWindows(false) + self.webView:navigationCallback(self.webViewMods) + -- self.webView:magnification(1) + self.webView:url("https://www.bbc.co.uk/sounds/play/live:bbc_radio_one_dance") + local iconImage = hs.image.imageFromPath("Spoons/BbcSounds.spoon/icon_16.png") + self.mbIcon = hs.menubar.new() + self.mbIcon:setIcon(iconImage) + self.mbIcon:setClickCallback(self.showHide) + local mbPos = self.mbIcon:frame() + local newPos = hs.geometry.point(mbPos.y, mbPos.x) + self.webView:topLeft(newPos) + self.eventTap = hs.eventtap.new({hs.eventtap.event.types.systemDefined}, self.handleMediaKey) + self.eventTap:start() + self.log.v('Startup complete.') + return self +end + +function obj.handleMediaKey(event) + local delete = false + + local data = event:systemKey() + + if data["down"] == false or data["repeat"] == true then + obj.log.v("Key event: " .. data["key"]) + if data["key"] == "PLAY" then + -- Handled automatically by WebView + -- obj.togglePlay() + delete = true + elseif data["key"] == "NEXT" then + obj.goLive() + delete = true + elseif data["key"] == "PREVIOUS" then + obj.goStart() + delete = true + end + end + + return delete, nil +end + +function obj.webViewMods(action, webView, navID, error) + if action == "didFinishNavigation" then + obj.hideHeader() + end +end + +function obj.hideHeader() + obj.webView:evaluateJavaScript("document.getElementById(\"orb-banner\").style.display = \"none\"") +end + +function obj.togglePlay() + obj.webView:evaluateJavaScript("document.getElementById(\"smphtml5iframesmp-wrapper\").contentWindow.document.getElementById(\"p_audioui_playpause\").click()") +end + +function obj.goLive() + obj.webView:evaluateJavaScript("document.getElementById(\"smphtml5iframesmp-wrapper\").contentWindow.document.getElementById(\"p_audioui_toLiveButton\").click()") +end + +function obj.goStart() + obj.webView:evaluateJavaScript("document.getElementById(\"smphtml5iframesmp-wrapper\").contentWindow.document.getElementById(\"p_audioui_backToStartButton\").click()") +end + +function obj.showHide(modifierKeys) + if obj.webView:isVisible() then + obj.webView:hide(0.3) + else + obj.webView:show(0.3) + obj.webView:bringToFront(true) + end +end + +function obj:stop() + self.eventTap:stop() + self.webView:delete() + self.mbIcon:delete() +end + + +return obj diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..1539cf0 --- /dev/null +++ b/init.lua @@ -0,0 +1 @@ +beebs = hs.loadSpoon("BbcSounds"):start()