From b8f0472e545929f9435f38af57f523f1b61bdf12 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Sun, 19 Dec 2021 19:56:17 +0100 Subject: [PATCH] Initial commit --- README.md | 14 ++++ Spoons/BbcSounds.spoon/icon_16.png | Bin 0 -> 384 bytes Spoons/BbcSounds.spoon/icon_32.png | Bin 0 -> 564 bytes Spoons/BbcSounds.spoon/init.lua | 99 +++++++++++++++++++++++++++++ init.lua | 1 + 5 files changed, 114 insertions(+) create mode 100644 README.md create mode 100644 Spoons/BbcSounds.spoon/icon_16.png create mode 100644 Spoons/BbcSounds.spoon/icon_32.png create mode 100644 Spoons/BbcSounds.spoon/init.lua create mode 100644 init.lua 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 0000000000000000000000000000000000000000..7d0abae7945d4601402fafcc7ba466940b5e651a GIT binary patch literal 384 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!73?$#)eFPFP;1lBNlUexRoq-tCyO!-LP#14WkY6yvoq$a7x{q7toA#X1?0S`A4wR_y zba4#fxSpJlkf1eX#sLPV#PauCjTy`CtmW31V=%h0-n!rv8-rxubV-RS?gKo}j-KLj zU^||>JZ1t9^YR?;9HEHaH$(Y1=&`NKwyKtHFe#p}vgxhZ1k3Fb5;?98M&jxp-sm#C zp47FE&8Xe+F literal 0 HcmV?d00001 diff --git a/Spoons/BbcSounds.spoon/icon_32.png b/Spoons/BbcSounds.spoon/icon_32.png new file mode 100644 index 0000000000000000000000000000000000000000..c6b2cc12b9d425549e1c9e75854ff012d46ca5a7 GIT binary patch literal 564 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnF3?v&v(vJfvmUKs7M+SzC{oH>NS%G}U;vjb? zhIQv;UIIB<0X`wFKADC8-5H2My=&R70(J401o;IsNZ!!nsr|O`<>6$HIiWJknt?{^ zc)B=-Xq-<@U|xJk4HlF`@6e68#(rAPpJR?&X;+I!;M&j89a}RpWoYidr8^% zccttr8lK$Uy{hd}YWaJ;2Cg-Gt2tNP{rc{%W(J)38zOMV8zI4IYWRd!-q~T7UDUFnEZpQxSV1y)H(@)~@`$AIoh%#=fRQ zu3k|*PUY*JS(T=!S#fb0ZMeTzh2e9ZafcJf5ho_67w>#m?7gj^7g6?|?+VYt-PfHO z`YyJ%N}SNQlx|fPNIYdBE|D))KCvd9rBkEj|IFu2Kg`7xl)^bP0l+XkK!;#$~ literal 0 HcmV?d00001 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()