Copy word list - vocabulary.com

Copy the list of words as plain text

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Copy word list - vocabulary.com
// @namespace   Violentmonkey Scripts
// @match       https://www.vocabulary.com/lists/*
// @grant       GM_addStyle
// @version     1.0
// @author      skygate2012
// @run-at      document-end
// @description Copy the list of words as plain text
// @license     WTFPL
// ==/UserScript==

const wordList = document.querySelector("#wordlist");

const copyButton = document.createElement("button");
copyButton.textContent = "Copy List";

copyButton.addEventListener("click", () => {
    const listItems = Array.from(wordList.children);
    const listString = listItems.reduce((str, el) => str += el.getAttribute("word") + "\n", "");

    navigator.clipboard.writeText(listString)
        .then(() => {
            toast("Word list copied to clipboard!");
        })
        .catch(err => {
            console.error("Failed to copy list: ", err);
        });
});

document.querySelector(".wordlist > .header").appendChild(copyButton);

function toast(message) {
    let toastContainer = document.querySelector(".toast-container");
    if (!toastContainer) {
        toastContainer = document.createElement("div");
        toastContainer.className = "toast-container";
        document.body.appendChild(toastContainer);
    }

    const toast = document.createElement("div");
    toast.className = "toast";
    toast.textContent = message;

    toastContainer.appendChild(toast);

    setTimeout(() => {
        toast.classList.add("show");
    }, 100);

    setTimeout(() => {
        toast.classList.remove("show");
        setTimeout(() => {
            toastContainer.removeChild(toast);
            if (toastContainer.childElementCount === 0) {
                document.body.removeChild(toastContainer);
            }
        }, 300);
    }, 2000);
}

GM_addStyle(`
  /* Toast container */
  .toast-container {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 9999;
  }

  /* Toast notification */
  .toast {
    background-color: #000;
    color: #fff;
    padding: 10px 20px;
    border-radius: 6px;
    opacity: 0;
    transition: opacity 0.3s;
  }

  /* Show the toast notification */
  .toast.show {
    opacity: 1;
  }
`);