| @@ -0,0 +1,67 @@ | |||
| 1 | + | // ==UserScript== | |
| 2 | + | // @name Cycle through color palettes with dot key | |
| 3 | + | // @namespace Violentmonkey Scripts | |
| 4 | + | // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAGoUlEQVR42rWXS2xdVxWGv7X3edx7bceJSRo7iRPioAQaoko8g6gYMMgAqQhVqpAqZqWDigESpYiZ1SktQ4QoyrQIkBgwYYJQhFRCE0vIzctpHo4d53HtWz/ie+7jnLP3YnDuTa7taxtR2NLS0ZH23utfa/3rsWWSSfM2b/vXjvxyZIC9P/UqryjuuKIGEP43SwXxgp01on9MWH33/MKby5NMGgF4Y/RXXwhs+U+BiT6faRuvjv/HMmIJJSb36Uzumi//+vGPbshrR347Enu9VLIDJ1o+yUADkMJy+fQuUN38J3nJDIQtl9xpG/laEHn/VmwHTrR8PQMJu1pFBJ97nNcdFewEUAFjDCYA9dAxLGz5ehbbgRPqkrcCkFcz31Yg6N6oTslbGaWRMqWyxfudlGihaTMqVYy1tBpt0jWPjQUM3b1BoVNeDUQY9zh56m+vGCucfPMs+75xhG+eqHB4b4jXp4HZsBpJA68bEXqvxHHE7N1ZpqamWLsRcu/POeq7d4h4HCKMB71eFBHyZsapn5zl+A/O0PykydV1OHo0pryNr6Xk8b0uUsVay/r6OjdmrxINWcbPGVDlzh8cQaUbjuK46XWbzz3RSJkDL47TWm5ivLKe5HzwcR3VwjJVNojLfI84XK7kmePiP/5JM2lhMKRrysgZS7QHvNtIHLOZNRIaRApXKVAKhfurOTPVNsZ0kkP6C0AcR1y5coVqtUocR6hqoVBAArbwxfSlbm88FeJA+NdCi1riCmDaL92UKIpYWFjg+vXrxHG8MTR97u4PABB0w27psPri3QaZ0y25p524J0nC5cuXMcb0Bdi3OPVTn/oYrxZBEVFUldAKqw3H1Hyzm2UbLzKGqakp6vU61lq896gWZ40xhGG4OwBVITAZx/bcohw0SH1E6gowqkophNtLbWZr6dNQqCpxHDMz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aNU9QFsR2DVA1BDh19wPQ90MT/7zt8wwItcOFcqy0UoFWsMvbJ+t14pb5TsQQDRU86PFeHpo4dC55P0iNeUdc45WSHdzwNPMKRkCCXbpJd+Jkh+HOb36aDYYtl9xJjXnHnF94fVl99lLm2zOxqYRGAtn8mvs00ssZI4HEphJmvj2jPnvp/MLry3aSSfNu/WdLZ4a/9buA0KvKGOjwLqb9l89zcxfy3zRk9YfvPfzx/Ukmzb8BY/WXJ90QXEwAAAAASUVORK5CYII= | |
| 5 | + | // @version 1.0.0 | |
| 6 | + | // | |
| 7 | + | // @match https://sudokupad.app/* | |
| 8 | + | // @grant none | |
| 9 | + | // | |
| 10 | + | // @author Marcus Jaschen | |
| 11 | + | // @description Add a keyboard shortcut which allows cycling through the three color palettes. | |
| 12 | + | // ==/UserScript== | |
| 13 | + | ||
| 14 | + | ||
| 15 | + | (function () { | |
| 16 | + | 'use strict'; | |
| 17 | + | ||
| 18 | + | const PALETTE_BUTTON_SELECTOR = 'button.palettebutton[data-control="selectpalette"]'; | |
| 19 | + | ||
| 20 | + | // SudokuPad creates the palette button only while the color entry method is | |
| 21 | + | // active (ToolColor.createPaletteButton() / .removePaletteButton()), so the | |
| 22 | + | // presence of the button in the DOM is the mode check. | |
| 23 | + | const getPaletteButton = () => { | |
| 24 | + | const button = document.querySelector(PALETTE_BUTTON_SELECTOR); | |
| 25 | + | if (button === null) return null; | |
| 26 | + | const visible = button.offsetParent !== null || button.getClientRects().length > 0; | |
| 27 | + | return visible ? button : null; | |
| 28 | + | }; | |
| 29 | + | ||
| 30 | + | const isTypingTarget = target => { | |
| 31 | + | if (!(target instanceof Element)) return false; | |
| 32 | + | if (target.isContentEditable) return true; | |
| 33 | + | return /^(input|textarea|select)$/i.test(target.tagName); | |
| 34 | + | }; | |
| 35 | + | ||
| 36 | + | const isDialogOpen = () => document.querySelector('.dialog-overlay') !== null; | |
| 37 | + | ||
| 38 | + | const isDotKey = event => event.key === '.' || event.code === 'NumpadDecimal'; | |
| 39 | + | ||
| 40 | + | // The button's handler is registered via addDownEventHandler(), i.e. on | |
| 41 | + | // 'mousedown'/'touchstart' — element.click() would not trigger it. | |
| 42 | + | const pressPaletteButton = button => { | |
| 43 | + | button.dispatchEvent(new MouseEvent('mousedown', { | |
| 44 | + | bubbles: true, | |
| 45 | + | cancelable: true, | |
| 46 | + | view: window, | |
| 47 | + | button: 0, | |
| 48 | + | })); | |
| 49 | + | }; | |
| 50 | + | ||
| 51 | + | const handleKeyDown = event => { | |
| 52 | + | if (!isDotKey(event)) return; | |
| 53 | + | if (event.repeat) return; | |
| 54 | + | if (event.ctrlKey || event.altKey || event.metaKey) return; | |
| 55 | + | if (isTypingTarget(event.target)) return; | |
| 56 | + | if (isDialogOpen()) return; | |
| 57 | + | ||
| 58 | + | const button = getPaletteButton(); | |
| 59 | + | if (button === null) return; | |
| 60 | + | ||
| 61 | + | event.preventDefault(); | |
| 62 | + | event.stopPropagation(); | |
| 63 | + | pressPaletteButton(button); | |
| 64 | + | }; | |
| 65 | + | ||
| 66 | + | document.addEventListener('keydown', handleKeyDown, {capture: true}); | |
| 67 | + | })(); | |
mjaschen / Sudokupad: Cycle Color Palette with Dot Key
Last active 12 hours ago