GitHub Label Keyboard Shortcuts

Add keyboard shortcuts for adding and removing labels on GitHub

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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

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.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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;
      }
    }
  });
})();