bandcamp: spacebar to play

2021-11-21

word count: 177

approx reading time: 1 mins

i'm used to music websites playing/pausing when i press spacebar. bandcamp does not do that. i got tired of feeling really silly every time i press the spacebar and only get scroll, so i made a really small greasemonkey script that fixes both things

it makes it so the play/pause button is clicked when the spacebar is pressed, and it also prevents the page from scrolling. if you want to enable this second behavior, remove the second addEventListener from the script below

this should work on every bandcamp page, even the ones that aren't on a .bandcamp.com domain

// get the bandcamp button
const button = document.querySelector(".play_cell a .playbutton");
// only do stuff if the button exists
if (button) {
	// click the button when the spacebar is released
  document.addEventListener('keyup', event => {
    if (event.code === 'Space') {
      button.click();
    }
  });
  // prevent default when spacebar is pressed, so we don't get page scroll
  window.addEventListener('keydown', function(e) {
    if(e.code == 'Space' && e.target == document.body) {
      e.preventDefault();
      return false;
    }
  });
}

note: if you are using tampermonkey, set @match to https://*/*, otherwise it seems to not work

separator line, for decoration