fwextensions / QuicKey

Jump between recent tabs in Chrome via keyboard shortcuts or a most recently used (MRU) menu.
https://fwextensions.github.io/QuicKey
217 stars 22 forks source link

Slightly (un)related question : disable Ctrl-w in Chrome devTools // 'possible for ffox also?' #67

Closed cg-cri-gl closed 2 years ago

cg-cri-gl commented 2 years ago

Hey,

Thanks for your effort on this extension, and also for listening to user-requests (e.g. #60 and #50 ).

What follows is a simple question for someone with your level of experience in developing extensions namely : I'd like to map ctrl-w (close Tab) in chrome to a noop, meaning it should not close the current Tab - without using any extension at all. The reason for this is : when sitting an online exam which offers a browser-based Terminal program, my muscle memory has a say about hitting 'ctrl-w' (which is delete word backwards in bash).

This is not much related to your extension, realise that - but if you could provide please some command (for devTools) to do this, maybe along the lines of your workaround here for Ctrl-Tab, ( which, unfortunately, does require some extension to be installed & enabled) - would be much appreciated -- Thanks !

fwextensions commented 2 years ago

I don't know of a way of modifying the ctrl-w shortcut, other than mapping it to an extension command. Seems like there are some extensions whose only job is doing just that: https://superuser.com/questions/569248/disable-hardwired-chrome-hot-key-ctrlw#comment2447171_1207752

If you're using Gnome, this person found a way to block the shortcut for exactly the same exam-related reason: https://suraj.io/post/disable-ctrl-w/#:~:text=Steps%20to%20disable%20%E2%80%9CCtrl%20%2B%20W%E2%80%9D&text=Goto%20the%20bottom%20of%20it,Shortcut%20to%20provide%20key%20binding.

cg-cri-gl commented 2 years ago

Thanks for the links you provided, I had already tried to do my homework and came across them already :) , but unfortunately, during some online exams, they ask you to disable all extensions except the one provided by them. As for using Linux and any window-manager , well then it's doable in more ways than one. This is however not in question :(

To elaborate on the previous idea about writing some dummy code to hijack ctrl-w : based on bits (pun not intended:) ) and pieces I've gathered from around the net, the way of disable the 'dreaded' ctrl-w would be as simple as :

  1. writing some code similar to
    [...]
    "commands": {
        "do-nothing": {
            "suggested-key": {
                "default": "Ctrl-W",
            },
            "description": "Do nothing!"
    [...]

and

  1. chrome.commands.onCommand.addListener(function(command) {
    if (command === "do-nothing") {
        console.log("doing nada!"); 
        }
    })

and put this code in 2 files:

however, the requirement is to only use the developerTools console in chrome. Now here comes my question: (how) would it be possible (I've tried a simple copy & paste but it didn't work) to somehow copy and paste this code in the console and have it accomplish what this code would accomplish if it were packaged in an extension?

More specific: how to declare the function do-nothing in the same portion of code, in the developerTools chrome console, ( as a js portion of code, not in json format in a manifest file ?

Appreciate your time !

fwextensions commented 2 years ago

I don't know of any way to generate a fake extension in devtools.

I think your best bet is to look for something outside of Chrome to block the key. If you're on Windows, you can use AutoHotKey to intercept the shortcut. On Mac, you might be able to use Hammerspoon.

cg-cri-gl commented 2 years ago

This would mean - to my limited understanding - that one would need to code a (fake) extension - and it isn't possible to simply declare a noop function in devTools console , and then assign a key to it :( , i.e. 'convert' the manifest.json file into a form which can be put in the console - Thanks ?

fwextensions commented 2 years ago

If there were APIs for that, it would be possible, but I haven't seen any evidence for them, and there certainly aren't any official ones.

You could open devtools on the exam page and execute this:

onbeforeunload = (e) => { return true; }

That will at least throw a confirmation dialog if you accidentally hit ctrl-W. But I haven't been able to override that shortcut on the page itself.

cg-cri-gl commented 2 years ago

Thanks for looking further into this ! The workaround you presented is very suitable for the exam scenario !

fwextensions commented 2 years ago

Good luck on the exam!

cg-cri-gl commented 2 years ago

Some last questions about the disabling / remapping unwanted default key shortcuts - although issue is already closed - if you don't mind:

Thanks !

cg-cri-gl commented 2 years ago

Thinking that, since issue is closed, you didn't get notified about my previous comment , hence reopening

fwextensions commented 2 years ago

Below is a quick example of writing an event listener to try to intercept some built in ctrl shortcuts. It's easy to add more case statements to filter other keys. But some just don't seem to work. This will suppress opening the Print dialog, but ctrl-N and ctrl-T don't get blocked. I suspect this is because, like ctrl-W, these shortcuts open or close windows and tabs, and Chrome doesn't want a webpage to be able to block that functionality. (You can override these shortcuts with an extension, but then it's the user choosing to do that, not some random page.)

addEventListener("keydown", event => {
  if (event.ctrlKey) {
    switch (event.code) {
      case "KeyP":
      case "KeyN":
      case "KeyT":
        console.log("Blocked", event.code);
        event.preventDefault();
        event.stopPropagation();
        break;
    }
  }
}, true);
cg-cri-gl commented 2 years ago

I see, Thanks ! One last question before the closing (final close :) ) so as to be able to quickly enter it in the devTools console at the beginning of the exam: would you be able to provide a simple one-liner (such as the one you provided before - onbeforeunload = (e) => { return true; } ) for Ctrl-P and Ctrl-N - Ctrl-T doesn't 'hurt' that much (is seldom used in bash ), please , or at least where to look for the equivalent of onbeforeunload which is specific to closing Tab iiuc ?

Thank you for your inputs & answers along the way !

fwextensions commented 2 years ago

Are you not able to copy and paste into the devtools? If you have to type it manually, this might be the shortest:

addEventListener("keydown", e => (e.ctrlKey && e.code == "KeyP") && e.preventDefault(), true)

But again, the N and T shortcuts can't be blocked, it seems.

cg-cri-gl commented 2 years ago

Then , if i got you correctly there isn’t any way to show a confirmation dialog popping up when pressing Ctrl-n/p ( akin to the solution you provided for Ctrl-w) - i guess i’ll have to live with some new pages and the print preview showing up during the exam when ( not if...) i’ll be pressing those shortcuts :) ; saying this since i can hardly rely on the level of web / js knowledge of the proctor and as such, he/she might not allow me to enter such “complicated” code in the devTools like the one you wrote in your previous comment, and even less copy and pasting. Too bad that something like onbeforeprint or onbeforenewpage doesn’t exist...

LE: found just now same problem with Ctrl-ShifT-C another Chrome shortcut (opens ... devTools (the irony ), which, in most modern linux Terminals (including the one from e.g. katacoda - exam simulator) has 'copy' meaning.

fwextensions commented 2 years ago

Again, ctrl-P will get blocked by that code snippet as written, but I don't think ctrl-N/T/C (and probably others) can be blocked without an extension.

cg-cri-gl commented 2 years ago

Understood that, thanks for underlining it - and I can confirm it works! But, since maybe I was not clear in my last question: I was actually wanting to know if there is a command which would only show the confirmation pop-up ("are you sure you want to ....?") , similar to the command you provided for Ctrl-w - onbeforeunload = (e) => { return true; } (repeating it for the sake of posterity who would read this Thread - I for one, would point people- who are preparing for exam especially - to it)

Why was my last question about displaying the confirmation dialog only when pressing e.g. Ctrl-P ? Because presumably that command (should it exist) would be more simpler to memorize and type in under time pressure.

In any case, I appreciate your feedback throughout this issue here. If you feel like closing it now, please feel free to do it, otherwise I will be closing it as soon as you get a change to reply to this last question about it being possible to show (only) the confirmation dialogwith a command such as on //_print / newpage_// = (e) => { return True; }. Thanks !

fwextensions commented 2 years ago

The onbeforeunload event doesn't have anything to do with cttrl-W. It fires whenever the tab is about to close, how ever that is triggered: a shortcut, a click on the closebox, a right-click, etc. There aren't similar events for preventing other actions (there's onbeforeprint, but it doesn't seem to let you prevent the print dialog opening; it's for tweaking the page layout before printing).

cg-cri-gl commented 2 years ago

Thank you! Closing now, with one last question: would it be possible to make these commands have effect for an entire window, especially for new Tabs which would be opened afterwards ? This would make it easier for an exam context, where one could enter the commands for diverting ctrl-w and ctrl-p prior to clicking the exam link (at which point the proctor would already be watching 'all the moves you make' :) )