GitHub Label Keyboard Shortcuts

Add keyboard shortcuts for adding and removing labels on GitHub

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         GitHub Label Keyboard Shortcuts
// @namespace    https://intuitiveexplanations.com/
// @version      2.0.1
// @description  Add keyboard shortcuts for adding and removing labels on GitHub
// @author       Radon Rosborough
// @match        https://github.com/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  "use strict";

  const tryUntilSuccess = (fn, totalTimeout, delay) => {
    return new Promise((resolve, reject) => {
      try {
        fn();
      } catch (err) {
        if (totalTimeout <= 0) {
          reject(err);
        } else {
          setTimeout(
            () =>
              tryUntilSuccess(fn, totalTimeout - delay, delay)
                .then(resolve)
                .catch(reject),
            delay,
          );
        }
        return;
      }
      resolve();
    });
  };

  const setLabelsMenuVisibility = (desiredState) => {
    if (document.getElementById("labels-select-menu").open !== desiredState) {
      document
        .getElementById("labels-select-menu")
        .querySelector("summary")
        .click();
    }
  };

  const setLabelPresence = (label, desiredState) => {
    const elt = document.querySelector(
      "form[aria-label='Apply labels'] label[data-prio-filter-value='waiting on response']",
    );
    if (elt.getAttribute("aria-checked") !== "" + desiredState) {
      elt.click();
    }
  };

  const shortcuts = [
    {
      matcher: (event) => {
        return (
          event.key === ";" && event.altKey && !event.ctrlKey && !event.metaKey
        );
      },
      label: "waiting on response",
      desiredState: true,
    },
    {
      matcher: (event) => {
        return (
          event.key === ":" && event.altKey && !event.ctrlKey && !event.metaKey
        );
      },
      label: "waiting on response",
      desiredState: false,
    },
  ];

  document.addEventListener("keydown", (event) => {
    for (const shortcut of shortcuts) {
      if (shortcut.matcher(event)) {
        setLabelsMenuVisibility(true);
        tryUntilSuccess(
          () => setLabelPresence(shortcut.label, shortcut.desiredState),
          1000,
          10,
        ).then(() => {
          setLabelsMenuVisibility(false);
          window.scrollTo(0, document.body.scrollHeight);
        });
        break;
      }
    }
  });
})();