Skip to content
TheChatArchiveThe Chat Archive
Googlegemma-3-27b-it· GoogleHugging Face LMArena Human Preference 140K

Programm a boockmarklet that adds a draggable (but also lockable) controller for all…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwen-max-2025-01-25’s answer.
  1. User

    Programm a boockmarklet that adds a draggable (but also lockable) controller for all arrow keys, as well as space bar and shift. It has to work with touchscreen and multi point input. It should give the underlying website the pressed buttons as keyboard inputs.

  2. Google Assistant

    Okay, this is a complex request! Here's a bookmarklet that attempts to fulfill your requirements. It's a significant amount of JavaScript, and I'll break down the explanation afterward. I'll also include important caveats and considerations.

    javascript:(function() {
      if (document.getElementById('virtualKeyboardController')) return; // Prevent multiple instances
    
      const controllerId = 'virtualKeyboardController';
    
      const controller = document.createElement('div');
      controller.id = controllerId;
      controller.style.position = 'fixed';
      controller.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
      controller.style.border = '1px solid #ccc';
      controller.style.padding = '10px';
      controller.style.borderRadius = '5px';
      controller.style.zIndex = '10000'; // Ensure it's on top
      controller.style.display = 'flex';
      controller.style.flexDirection = 'column';
      controller.style.alignItems = 'center';
      controller.style.pointerEvents = 'none'; // Initially, don't interfere with page clicks
    
      let isDragging = false;
      let dragStartX = 0;
      let dragStartY = 0;
    
      let isLocked = false;
    
      // Function to create a button
      function createButton(label, key, touchArea) {
        const button = document.createElement('button');
        button.textContent = label;
        button.style.margin = '5px';
        button.style.padding = '8px 12px';
        button.style.backgroundColor = '#eee';
        button.style.border = '1px solid #999';
        button.style.borderRadius = '3px';
        button.style.cursor = 'pointer';
        button.style.userSelect = 'none';
        button.style.width = touchArea ? '60px' : 'auto';
        button.style.height = touchArea ? '60px' : 'auto';
        button.style.lineHeight = touchArea ? '60px' : 'auto';
        button.style.textAlign = 'center';
        button.style.fontSize = touchArea ? '24px' : '14px';
    
        button.addEventListener('mousedown', function(e) {
          e.preventDefault(); // Prevent text selection
          if (!isLocked) {
            simulateKeyPress(key);
          }
        });
    
        button.addEventListener('touchstart', function(e) {
          e.preventDefault();
          if (!isLocked) {
            simulateKeyPress(key);
          }
        });
    
        return button;
      }
    
      // Create buttons
      const upButton = createButton('▲', 'ArrowUp', true);
      const downButton = createButton('▼', 'ArrowDown', true);
      const leftButton = createButton('◄', 'ArrowLeft', true);
      const rightButton = createButton('►', 'ArrowRight', true);
      const spaceButton = createButton('Space', ' ', true);
      const shiftButton = createButton('Shift', 'Shift', true);
      const lockButton = createButton('Lock', 'Lock', false);
    
      controller.appendChild(upButton);
      controller.appendChild(downButton);
      controller.appendChild(leftButton);
      controller.appendChild(rightButton);
      controller.appendChild(spaceButton);
      controller.appendChild(shiftButton);
      controller.appendChild(lockButton);
    
      lockButton.addEventListener('click', function() {
        isLocked = !isLocked;
        lockButton.textContent = isLocked ? 'Unlock' : 'Lock';
      });
    
      // Dragging functionality
      controller.addEventListener('mousedown', function(e) {
        isDragging = true;
        dragStartX = e.clientX - controller.offsetLeft;
        dragStartY = e.clientY - controller.offsetTop;
        controller.style.pointerEvents = 'auto'; // Enable pointer events during drag
      });
    
      document.addEventListener('mouseup', function() {
        isDragging = false;
        controller.style.pointerEvents = 'none'; // Disable pointer events after drag
      });
    
      document.addEventListener('mousemove', function(e) {
        if (isDragging) {
          controller.style.left = (e.clientX - dragStartX) + 'px';
          controller.style.top = (e.clientY - dragStartY) + 'px';
        }
      });
    
      controller.addEventListener('touchstart', function(e) {
        isDragging = true;
        dragStartX = e.touches[0].clientX - controller.offsetLeft;
        dragStartY = e.touches[0].clientY - controller.offsetTop;
        controller.style.pointerEvents = 'auto';
      });
    
      document.addEventListener('touchend', function() {
        isDragging = false;
        controller.style.pointerEvents = 'none';
      });
    
      document.addEventListener('touchmove', function(e) {
        if (isDragging) {
          controller.style.left = (e.touches[0].clientX - dragStartX) + 'px';
          controller.style.top = (e.touches[0].clientY - dragStartY) + 'px';
          e.preventDefault(); // Prevent page scrolling during drag
        }
      });
    
      // Function to simulate key press
      function simulateKeyPress(key) {
        const event = new KeyboardEvent('keydown', {
          bubbles: true,
          cancelable: true,
          key: key,
          code: key === 'Shift' ? 'ShiftLeft' : key, // Special case for Shift
          shiftKey: key === 'Shift',
        });
        document.dispatchEvent(event);
      }
    
      document.body.appendChild(controller);
    })();
    

    How to Use:

    1. Create a Bookmark: In your browser, create a new bookmark.
    2. Edit the Bookmark: Edit the bookmark and paste the entire JavaScript code above into the "URL" or "Address" field of the bookmark.
    3. Save the Bookmark: Save the bookmark.
    4. Activate: When you're on a webpage where you want the virtual keyboard, click the bookmark. The controller will appear.

    Explanation:

    1. Prevent Multiple Instances: The if (document.getElementById('virtualKeyboardController')) return; line prevents the bookmarklet from adding multiple controllers if you click it repeatedly.

    2. Controller Creation:

      • A div element with the ID virtualKeyboardController is created. This will be the container for the buttons.
      • CSS styles are applied to position it fixed (so it stays in place while scrolling), give it a background, border, padding, and a high z-index to ensure it's on top of other content.
      • pointerEvents: 'none'; is initially set to prevent the controller from interfering with clicks on the underlying webpage. This is changed during dragging.
    3. createButton Function: This helper function simplifies the creation of buttons. It takes the button label, the key to simulate, and a touchArea flag. The touchArea flag makes the buttons larger and more suitable for touch input.

    4. Button Creation: Buttons are created for ArrowUp, ArrowDown, ArrowLeft, ArrowRight, Space, Shift, and Lock. Event listeners are added to each button to call simulateKeyPress when clicked or touched.

    5. Lock Button: The lock button toggles the isLocked variable. When locked, the buttons won't send key presses.

    6. Dragging Functionality:

      • mousedown and touchstart event listeners are added to the controller to initiate dragging.
      • mouseup and touchend event listeners stop the dragging.
      • mousemove and touchmove event listeners update the controller's position during dragging.
      • pointerEvents is toggled to allow interaction during dragging and prevent it otherwise.
      • e.preventDefault() is used in touchmove to prevent page scrolling during dragging.
    7. simulateKeyPress Function:

      • This function creates a KeyboardEvent object.
      • bubbles: true and cancelable: true allow the event to propagate up the DOM tree and be canceled if necessary.
      • key is set to the key to simulate.
      • code is set to the appropriate key code. A special case is added for 'Shift' to use 'ShiftLeft'.
      • shiftKey is set to true if the 'Shift' key is being simulated.
      • document.dispatchEvent(event) dispatches the event, effectively sending the key press to the underlying webpage.
    8. Append to Body: Finally, the controller is appended to the document.body.

    Important Considerations and Caveats:

    • Security: Bookmarklets run in the context of the webpage you're visiting. Be cautious about using bookmarklets from untrusted sources, as they could potentially access sensitive information.
    • Website Compatibility: Not all websites will respond to keyboard events in the same way. Some websites may override or ignore the events generated by the bookmarklet. This is especially true for websites that use custom input handling or game engines.
    • Focus: The underlying webpage needs to have focus for the keyboard events to be received. If the focus is on the bookmarklet's controller, the events won't be sent to the webpage.
    • Event Propagation: Some websites may prevent the keyboard events from propagating to the intended elements.
    • Touch Input: Touch input is supported, but the touch areas might need adjustment depending on your screen size and preferences.
    • Shift Key: The Shift key simulation is basic. It only sets the shiftKey property of the event. Some websites may require more complex handling of the Shift key.
    • CSS Styling: The CSS styling is basic. You can customize it to match your preferences.
    • Performance: Creating and manipulating DOM elements can be performance-intensive. If you experience performance issues, you may need to optimize the code.
    • Conflicts: The bookmarklet might conflict with other JavaScript code running on the webpage.
    • Accessibility: The controller is not fully accessible. Consider adding ARIA attributes and keyboard navigation for better accessibility.
    • Browser Compatibility: The code should work in most modern browsers, but it's always a good idea to test it in different browsers.

    Possible Improvements:

    • Customization: Add options to customize the button labels, colors, and positions.
    • Accessibility: Improve accessibility with ARIA attributes and keyboard navigation.
    • More Keys: Add support for more keys, such as Ctrl, Alt, Tab, Enter, etc.
    • Layout: Allow users to choose different layouts for the controller.
    • Persistence: Store the controller's position in local storage so it remembers its position between sessions.
    • Error Handling: Add error handling to gracefully handle unexpected errors.
    • Debouncing: Implement debouncing to prevent rapid key presses from overwhelming the webpage.

    This is a starting point. You'll likely need to adjust the code to fit your specific needs and the websites you're using it with. Good luck!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.