Skip to content

Instantly share code, notes, and snippets.

@fitsum
Last active April 26, 2024 05:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitsum/15ba655c648790ebb43b00de1497d52a to your computer and use it in GitHub Desktop.
Save fitsum/15ba655c648790ebb43b00de1497d52a to your computer and use it in GitHub Desktop.
CSS transform selected text - NEEDS WORK
// From browser source/snippets
// TODO: use selection API
// IDEA: selecting -> uppercase => voices -> yelling one word at a time (AWS?)
//noting line breaks in chrome devtools between selection and post slice
//FIXME: better attach
document.styleSheets[document.styleSheets.length - 1].addRule("::selection", "background: red !important; color: pink !important; text-transform: uppercase !important");
//get whole parent
document.addEventListener('selectionchange', e => {
console.clear();
slice = document.getSelection().toString();
if (slice !== "") {
parent = document.getSelection().getRangeAt(0).commonAncestorContainer.textContent;
preSlice = parent.slice(0, parent.indexOf(slice))
postSlice = parent.slice(parent.indexOf(slice) + slice.length, parent.length - 1)
// otherStyle = 'font-weight: normal';
// sliceStyle = 'font-weight: bold';
// string = preSlice + '{{ ' + slice.toUpperCase() + ' }}' + postSlice;
string = preSlice + slice.toUpperCase() + postSlice;
console.log(string)
// string = "%c"+ preSlice + '%c' + slice + '%c' + postSlice;
// console.log(string, otherStyle, sliceStyle, otherStyle)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment