Greasy Fork

Quizlet Shortcuts Alt+V Clipboard Paste, Add Card, Scroll, Alt+c I was correct

Paste clipboard content, add a new card, focus fields, and scroll to the bottom, just start typing, alt+c i was correct

当前为 2025-07-02 提交的版本,查看 最新版本

// ==UserScript==
// @name         Quizlet Shortcuts Alt+V Clipboard Paste, Add Card, Scroll, Alt+c I was correct
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Paste clipboard content, add a new card, focus fields, and scroll to the bottom, just start typing, alt+c i was correct
// @author       bwhurd
// @match        https://quizlet.com/*
// @grant        none
// ==/UserScript==

// alt+c to click i was correct
// just type to go to type box
// alt+v paste question and answer separated by question mark in edit mode
// alt+z scrolls to bottom of page

(function() {
    'use strict';

    document.addEventListener('keydown', async function(e) {
        // Alt+V functionality: Paste and add a card
        if (e.altKey && e.key === 'v') {
            const activeElement = document.activeElement;

            if (!activeElement || activeElement.getAttribute('contenteditable') !== 'true') {
                alert("Focus must be on a contenteditable field.");
                return;
            }

            const clipboardText = await navigator.clipboard.readText();
            if (!clipboardText) {
                alert("Clipboard is empty.");
                return;
            }

            const parts = clipboardText.split(/\?(.+)/);
            if (parts.length < 2) {
                alert("No question mark found in clipboard text.");
                return;
            }

            const textBefore = parts[0] + '?';
            const textAfter = parts[1].trim();

            activeElement.innerHTML = textBefore;

            const contentEditables = Array.from(document.querySelectorAll('[contenteditable="true"]'));
            const currentIndex = contentEditables.indexOf(activeElement);
            if (currentIndex === -1 || currentIndex + 1 >= contentEditables.length) {
                alert("No next contenteditable field found.");
                return;
            }

            const nextField = contentEditables[currentIndex + 1];
            nextField.focus();
            nextField.innerHTML = textAfter;

            const addCardButton = document.querySelector('button[aria-label="Add a card"]');
            if (addCardButton) {
                addCardButton.click();
            } else {
                alert("Add a card button not found.");
                return;
            }

            setTimeout(() => {
                const updatedContentEditables = Array.from(document.querySelectorAll('[contenteditable="true"]'));
                if (updatedContentEditables.length >= 2) {
                    const secondToLastField = updatedContentEditables[updatedContentEditables.length - 2];
                    secondToLastField.focus();
                } else {
                    alert("Second-to-last contenteditable field not found.");
                }
            }, 500);
        }

        // Alt+Z functionality: Scroll to the bottom
        if (e.altKey && e.key === 'z') {
            window.scrollTo({
                top: document.body.scrollHeight,
                behavior: 'smooth' // Smooth scrolling effect
            });
        }
    });
})();


// type to go to type box
(function(){
  const selector = 'input[aria-label="Type the answer"]';
  window.addEventListener('keydown', function(e){
    const input = document.querySelector(selector);
    if (!input || document.activeElement === input) return;
    if (e.key.length === 1) {
      // focus before default so the character is inserted
      input.focus();
    }
    else if (e.key === 'Backspace') {
      e.preventDefault();
      input.focus();
      input.value = input.value.slice(0, -1);
      input.dispatchEvent(new Event('input',{bubbles:true}));
    }
  }, true);
})();


// alt+c to click "i was correct"
(function() {
    const buttonSelector = 'button.AssemblyLink.AssemblyLink--small.AssemblyLink--primary span';

    // Function to handle keyboard shortcut
    function handleShortcut(e) {
        if (e.altKey && (e.key === 'c' || e.key === 'C')) {
            const buttonSpan = document.querySelector(buttonSelector);
            if (!buttonSpan) return;

            const button = buttonSpan.closest('button');
            if (!button) return;

            button.click();
        }
    }

    document.addEventListener('keydown', handleShortcut);
})();