Programm a boockmarklet that adds a draggable (but also lockable) controller for all…
- 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.
- Assistant
Creating a bookmarklet that adds a draggable and lockable controller for arrow keys, space bar, and shift, while also supporting touchscreen and multi-point input, is a complex task. Below is a step-by-step explanation of how to achieve this, followed by the complete bookmarklet code.
Approach
-
Controller Design:
- Create a UI element (a virtual controller) that includes buttons for each key: arrow keys, space bar, and shift.
- Make the controller draggable so users can position it anywhere on the screen.
-
Touchscreen Support:
- Use
touchstart,touchmove, andtouchendevents to handle multi-touch interactions. - Simulate keyboard inputs when the user interacts with the virtual buttons.
- Use
-
Locking Mechanism:
- Add a toggle button to "lock" the controller in place, preventing it from being dragged.
-
Keyboard Input Simulation:
- Use the
KeyboardEventAPI to simulate key presses (keydown) and releases (keyup) for the underlying website.
- Use the
-
Bookmarklet Constraints:
- Bookmarklets are limited to JavaScript code and must be encapsulated in a single line.
- The code must be self-contained and not rely on external libraries.
Bookmarklet Code
Here is the complete bookmarklet code:
javascript:(function(){var s=document.createElement('style');s.innerHTML='.controller{position:absolute;bottom:20px;left:20px;background:#333;color:#fff;padding:10px;border-radius:10px;z-index:9999;touch-action:none}.button{background:#555;color:#fff;padding:10px 20px;margin:5px;border-radius:5px;cursor:pointer}.button.active{background:#f00}.lock{position:absolute;top:5px;right:5px;background:#ff0;color:#000;padding:5px;border-radius:5px;cursor:pointer}';document.head.appendChild(s);var c=document.createElement('div');c.className='controller';c.innerHTML='<div class="lock">🔒</div><div class="button" data-key="ArrowUp">↑</div><div class="button" data-key="ArrowLeft">←</div><div class="button" data-key="ArrowDown">↓</div><div class="button" data-key="ArrowRight">→</div><div class="button" data-key="Space">Space</div><div class="button" data-key="Shift">Shift</div>';document.body.appendChild(c);var isLocked=false;c.querySelector('.lock').addEventListener('click',function(){isLocked=!isLocked;this.textContent=isLocked?'🔓':'🔒';});var pos={x:0,y:0};c.addEventListener('touchstart',function(e){if(isLocked)return;var t=e.touches[0];pos.x=t.clientX-c.offsetLeft;pos.y=t.clientY-c.offsetTop;e.preventDefault();});c.addEventListener('touchmove',function(e){if(isLocked)return;var t=e.touches[0];c.style.left=(t.clientX-pos.x)+'px';c.style.top=(t.clientY-pos.y)+'px';e.preventDefault();});c.querySelectorAll('.button').forEach(function(b){b.addEventListener('touchstart',function(e){simulateKey(b.dataset.key,true);b.classList.add('active');e.preventDefault();});b.addEventListener('touchend',function(e){simulateKey(b.dataset.key,false);b.classList.remove('active');e.preventDefault();});});function simulateKey(key,isDown){var event=new KeyboardEvent(isDown?'keydown':'keyup',{key:key,bubbles:true,cancelable:true});document.activeElement.dispatchEvent(event);}})();
Explanation of the Code
-
Styling:
- The
.controllerclass styles the draggable container. - The
.buttonclass styles individual buttons for each key. - The
.lockclass provides a toggle button to lock/unlock the controller.
- The
-
Controller Creation:
- A
divelement is created dynamically and appended to the document body. - Buttons for arrow keys, space bar, and shift are added as child elements.
- A
-
Draggable Functionality:
- The
touchstartevent captures the initial touch position relative to the controller. - The
touchmoveevent updates the controller's position based on touch movement unless it is locked.
- The
-
Locking Mechanism:
- Clicking the lock button toggles the
isLockedstate, preventing further dragging.
- Clicking the lock button toggles the
-
Simulating Key Presses:
- The
simulateKeyfunction creates and dispatcheskeydownorkeyupevents for the specified key. - These events are sent to the currently focused element, simulating real keyboard input.
- The
-
Touch Events:
- Each button listens for
touchstartandtouchendevents to simulate pressing and releasing the corresponding key.
- Each button listens for
How to Use the Bookmarklet
- Copy the entire bookmarklet code above.
- Create a new bookmark in your browser.
- Paste the code into the URL field of the bookmark.
- Save the bookmark.
- Navigate to any website and click the bookmarklet to activate the controller.
Limitations
- Some websites may block simulated keyboard events for security reasons.
- Multi-touch gestures (e.g., pinch-to-zoom) may interfere with the controller.
- The controller is styled minimally and may need adjustments for specific use cases.
This bookmarklet provides a robust solution for adding a virtual controller to any website, supporting both touch and keyboard simulation.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.