Skip to content

Instantly share code, notes, and snippets.

@itswin
Created March 6, 2025 05:17
Show Gist options
  • Select an option

  • Save itswin/db7d05c02a3158a3fe6c0b983fe4c867 to your computer and use it in GitHub Desktop.

Select an option

Save itswin/db7d05c02a3158a3fe6c0b983fe4c867 to your computer and use it in GitHub Desktop.
Copy Selected Cells in Sudokupad
// ==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