(Tool) Detecting when a node is selected in the editor?

Godot 4 update: EditorInterface is now a singleton (see get_editor_interface deprecation message)

var editor_selection := EditorInterface.get_selection()

if self in editor_selection.get_selected_nodes():
    # do stuff only if a node with this tool script attached

# or if you need to go further
for selected_node in editor_selection.get_selected_nodes():
    # do stuff with each selected node

I see comments saying that using Editor classes breaking build, and a trick to use Engine.get_singleton("EditorInterface") instead on this thread: How to strip editor specific code from build in godot 4.2, but I have not observed such error in Godot 4.2.1 nor 4.3 on Linux.