Above code will fail when console is undocked and when the window sizes are manipulated and when page is loading.
You may also get false positives when working with certain frame setups.
The bottom line is that there is no documented way to reliably detect the inspector, and no way at all to detect it when undocked. This is most likely due to security concerns. A malicious webpage could conceal its payload when the inspector was open, making it more difficult to debug.