Assign keyboard shortcut to pause music in website

2

2

I use saavn.com for listening to bollywood music. It's a great service but they don't have native mac app so I can't control it using keyboard media keys.

There must be a way to use keyboard media keys to control all music.

Amit Vaghela

Posted 2013-05-16T02:00:05.133

Reputation: 123

1

It's generally difficult/impossible to intercept the special KB media keys. Using a custom userscript you should be able to inject JS into the page that will assign keyboard shortcuts to the various play/pause etc. functions. I doubt such a script already exists for this site, but you never know. There might also be scripts for similar sites that can be suitably modified.

– Karan – 2013-05-16T02:03:38.033

Answers

6

You could assign a shortcut to a script like this:

tell application "Safari"
    repeat with t in documents
        tell t
            if URL starts with "http://www.saavn.com/" then
                do JavaScript "e = document.querySelectorAll('#pause:not(.hide)')[0] || document.querySelectorAll('#play:not(.hide)')[0]; e.click()"
                exit repeat
            end if
        end tell
    end repeat
end tell

A similar script for YouTube and Chrome:

tell application "Google Chrome"
    repeat with t in tabs of windows
        tell t
            if URL starts with "http://www.youtube.com" then
                execute javascript "player = document.querySelectorAll('#player embed')[0]
if (player) {
    player.getPlayerState() == 1 ? player.pauseVideo() : player.playVideo()
} else { // if youtube.com/html5 is enabled
    document.querySelectorAll('.html5-player-chrome > button:first-child')[0].click()
}"
                exit repeat
            end if
        end tell
    end repeat
end tell

Lri

Posted 2013-05-16T02:00:05.133

Reputation: 34 501

Can't upvote this enough. – dev – 2015-03-03T22:31:29.657

I couldn't get this script to work for Safari nor the one you posted in AskDifferent. But the script in your github page worked fine. You might want to update the answer. – Alex B – 2015-06-16T17:29:08.717

the query selector string no longer works. use embed#movie_player. – fent – 2013-10-14T18:42:03.217

@thisisadeadend Thanks, I replaced #watch7-player embed with #player embed. – Lri – 2013-10-14T18:51:02.733

1

I've made a few more scripts for some common youtube actions and put them up here https://gist.github.com/fent/7763775 enjoy

– fent – 2013-12-06T17:55:20.400

0

For the keyboard part I would use keyremap4macbook - this will allow you to intercept media keys. I can also launch scripts for keypresses (@lauri-rantas answer covers that nicely). Unfortunately it also needs some coding to adjust it to ones needs.

What I like about it is:

  • you will even be to override the media keys and "caps-lock"
  • one can also differenciate between left-shift, control, command keys and the corresponding right ones
  • the remappings can depend on which app is in foreground - so your mapping won't interfere with other media players

bdecaf

Posted 2013-05-16T02:00:05.133

Reputation: 458