My Choices

Transform default HTML <select> tags into Choices.js dropdowns for an enhanced user experience.

目前為 2023-04-03 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         My Choices
// @namespace    https://github.com/iCross
// @version      0.1.1
// @description  Transform default HTML <select> tags into Choices.js dropdowns for an enhanced user experience.
// @icon         https://raw.githubusercontent.com/iCross/my_choices/main/icon.jpg
// @license      MIT
// @author       https://github.com/iCross
// @homepageURL  https://github.com/iCross/my_choices
// @supportURL   https://github.com/iCross/my_choices/issues
// @match        *://*/*
// @grant        GM_registerMenuCommand
// @require      https://cdn.jsdelivr.net/npm/[email protected]/public/assets/scripts/choices.min.js
// @require      https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1
// ==/UserScript==
 
(function () {
  "use strict";
 
  // console.log("Userscript is running.");
  // Add Choices.js CSS to the document
  function addChoicesCSS() {
    const choicesCSSLink = document.createElement("link");
    choicesCSSLink.href =
      "https://cdn.jsdelivr.net/npm/[email protected]/public/assets/styles/base.min.css";
    choicesCSSLink.rel = "stylesheet";
    console.log("Adding Choices.js CSS");
    document.head.appendChild(choicesCSSLink);
 
    const customStyle = document.createElement("style");
    customStyle.textContent = `
    .choices__list--dropdown,
    .choices__list[aria-expanded] {
      word-break: break-word;
      width: max-content;
    }
  `;
    document.head.appendChild(customStyle);
  }
 
  // Replace default select menus with Choices.js
  function applyChoices() {
    console.log("Applying Choices.js.");
 
    document.querySelectorAll("select").forEach(function (select) {
      if (!select._choicesInstance) {
        // Create a new Choices instance with the 'itemSelectText' set to an empty string
 
        new Choices(select, {
          itemSelectText: "",
        });
      }
    });
  }
 
  const { register } = VM.shortcut;
 
  register("c-i", () => {
    console.log("You just pressed Shortcut in violetmonkey.");
 
    // Add Choices.js CSS when the keyboard shortcut is pressed
    addChoicesCSS();
 
    // Run once when the script is loaded
    applyChoices();
  });
 
  // Register a menu command to trigger the same function
  GM_registerMenuCommand("Apply Choices.js", () => {
    addChoicesCSS();
    applyChoices();
    console.log("Choices.js applied via menu command.");
  });
})();