BC: keyboard shortcuts

for Bandcamp and its embeded player: seek, autoseek when changing track, play/pause. Designed for those digging through lots of tunes.

目前为 2022-10-10 提交的版本。查看 最新版本

// ==UserScript==
// @name         BC: keyboard shortcuts
// @description  for Bandcamp and its embeded player: seek, autoseek when changing track, play/pause. Designed for those digging through lots of tunes.
// @namespace    userscript1
// @grant        none
// @version      1.3.2
// @match        https://bandcamp.com/*
// @match        https://*.bandcamp.com/*
// @match        https://*/*
 // @license     GPLv3
// ==/UserScript==

(function() {
  'use strict';

  // Key_ settings refer to physical positions as if you had a QWERTY layout, not letters.
  // https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_code_values
  const playPause   = 'KeyP';
  const prev        = 'KeyI';
  const next        = 'KeyO';
  const prevAndSeek = 'KeyH';
  const nextAndSeek = 'KeyL';
  const seekBack    = 'KeyJ';
  const seekForward = 'KeyK';
  const initialSeek = 60;
  const manualSeek  = 30;
  // end configuration

  // only run on *bandcamp.com or bandcamp on a custom domain
  if (!document.location.hostname.endsWith('bandcamp.com')
      && !document.head.querySelector('meta[property="twitter:site"][content="@bandcamp"]') ) {
        return;
  }

  var prevButton, nextButton, playButton;
  const aud = document.querySelectorAll('audio')[0];
  if (!aud) { return; }

  window.addEventListener('keydown', (evt) => {
      if ($('.ui-widget-overlay')) {
        // dialog box is open
        return;
      }

      // check every time to allow collection page to work
      if (!findButtons() ) {
        return;
      }

      // console.log(evt.code);  // uncomment to check key codes
      switch(evt.code) {
          case prev:
              prevButton.click();
              scrollEmbedPlayer();
              break;
          case prevAndSeek:
              prevButton.click();
              aud.currentTime = initialSeek;
              scrollEmbedPlayer();
              break;
          case next:
              nextButton.click();
              scrollEmbedPlayer();
              break;
          case nextAndSeek:
              nextButton.click();
              aud.currentTime = initialSeek;
              scrollEmbedPlayer();
              break;
          case seekBack:
              aud.currentTime -= manualSeek;
              break;
          case seekForward:
              aud.currentTime += manualSeek;
              break;
          case playPause:
              playButton.click();
              if (playPause === 'Space') {
                evt.preventDefault();  // prevent page scroll
              }
              break;
      }
  }, false);

  function findButtons() {
    prevButton = $('div.prevbutton') || $('div.prev-icon');
    nextButton = $('div.nextbutton') || $('div.next-icon');
    playButton = $('div.playbutton') || $('div.playpause') || $('div#big_play_button');
    return playButton;
  }

  function scrollEmbedPlayer() {
    $('li.currenttrack')?.scrollIntoView({behavior: 'smooth', block: 'nearest', inline: 'nearest'});
  }

  function $(s) {
    return document.querySelector(s);
  }  

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址