如何实现对window.getSelection的选择内容变更订阅?
window.getSelection() Changes Hey there! I totally get wanting a clean way to track when the user's text selection changes—those workarounds with timers or manually listening to every mouse/key event are pretty clunky and far from ideal.
The bad news is that browsers don't have a native API that lets you directly subscribe to selection changes right now. So the brute-force methods you're thinking of are the only native options, but they're definitely not great.
The good news? I've created a library called selection-range-enhancer that handles this problem smoothly. It takes care of all the low-level event listening and edge cases under the hood, giving you a straightforward way to listen for updates to the selection range without the hassle of writing all that boilerplate yourself. It's currently the best solution I know of for this use case.
内容的提问来源于stack exchange,提问作者Nik

