Created
March 6, 2025 05:17
-
-
Save itswin/db7d05c02a3158a3fe6c0b983fe4c867 to your computer and use it in GitHub Desktop.
Copy Selected Cells in Sudokupad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name Copy Selected Cells | |
| // @include https://sudokupad.app/* | |
| // @version 1 | |
| // @grant none | |
| // ==/UserScript== | |
| (function () { | |
| function injectScript() { | |
| const script = document.createElement('script'); | |
| script.textContent = ` | |
| document.addEventListener('keydown', function(event) { | |
| if (event.ctrlKey && event.shiftKey && event.key === 'C') { | |
| const selectedCells = Framework.app.puzzle.selectedCells; | |
| const rows = new Set(selectedCells.map(cell => cell.row)); | |
| const cols = new Set(selectedCells.map(cell => cell.col)); | |
| let sorter = (a,b)=>(a.col+a.row*9 - b.col-b.row*9); | |
| let cells = selectedCells; | |
| if (rows.size == 9 && cols.size == 9 && selectedCells.length == 17) { | |
| // Handle the one row one col case. | |
| const mostCommonRow = Object.entries(selectedCells.reduce( | |
| (acc, { row }) => (acc[row] = (acc[row] || 0) + 1, acc), {}) | |
| ).reduce((max, curr) => curr[1] > max[1] ? curr : max)[0]; | |
| const mostCommonCol = Object.entries(selectedCells.reduce( | |
| (acc, { col }) => (acc[col] = (acc[col] || 0) + 1, acc), {}) | |
| ).reduce((max, curr) => curr[1] > max[1] ? curr : max)[0]; | |
| const row = selectedCells.filter(cell => cell.row == mostCommonRow).sort(sorter); | |
| const col = selectedCells.filter(cell => cell.col == mostCommonCol).sort(sorter); | |
| cells = row.concat(col); | |
| } else { | |
| cells = selectedCells.sort(sorter); | |
| } | |
| const values = cells.map(cell => cell.value || cell.given).join(''); | |
| navigator.clipboard.writeText(values); | |
| } | |
| }); | |
| `; | |
| document.head.appendChild(script); | |
| } | |
| injectScript(); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment