2

I am creating a script to register where users are making CTRF + F in my website. For this I made a code that look every time the user is calling the browser's find and I push into an array the name of the page where he did it. Some of my page are in reality tabs that mean that the browser's find doesn't close it self when I change tab. So I would like to have a script to close the browser's when I change tab or page beacuse if not the browser's find could stay open even if the user change tab and do an other research. For now I have this:

var ctrlf = []; 
var url = '';
window.addEventListener("keydown", (event) => {
url = url_parse();
  if ((event.ctrlKey && (event.key == "f")) || (event.key == "F3")) {
    ctrlf.push(url_parse());
//url_parse is a function to parse the url of the page into a string (see this as the id of a page / tab)
  }
document.addEventListener("click", (event) => {
  if(url != url_parse()){
     var e = new KeyboardEvent('keypress', { 'key': 'Escape' })
     document.dispatchEvent(e)
  }
});

My idea is to simulate the keypress of Escape but this is not working.

Any ideas to improve my code is welcome or new ideas on how to close the browser's find when the user change tab (only the hash of the url change when the user change tab).

Thanks

j08691
  • 204,283
  • 31
  • 260
  • 272
Ketchup
  • 150
  • 11
  • Does this answer your question? [Is it possible to simulate key press events programmatically?](https://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programmatically) – Nino Filiu Oct 29 '20 at 14:13
  • No I already tried this its not working – Ketchup Oct 29 '20 at 14:18
  • 1
    Could you describe a bit more what you mean by not working. Are you saying the key press event is not fired on a click? Also, do you see the lowercase f as keydown event says which key has been pressed ( not the resulting character)? – A Haworth Oct 29 '20 at 15:04
  • I see the event been created ( I can log it) but the browser's find is not closing as if it would if I am pressing the ESC key manually – Ketchup Oct 30 '20 at 08:55

1 Answers1

0

you could do something like

document.addEventListener("click", (event) => {
  if(url != url_parse()){
     window.close();
  }

this is as per the Mozilla Docs

Edit: As of 2020, due to security concerns, most modern browsers do not allow this action anymore. Please visit this link for more information.

berlin
  • 506
  • 4
  • 14
  • I just tried it this is not working, the browser is blocking the close. I recieved the waring : "Scripts may close only the windows that were opened by them." – Ketchup Oct 30 '20 at 09:00
  • sorry - i found out this only works for windows opened by the javascript. as of [this post](https://stackoverflow.com/a/19768082/13448177), wee cannot do this anymore. You wlil hve to open the window and then close it. it is a security feature in the latest browsers. – berlin Oct 30 '20 at 09:26
  • That for the advice, but the browser's find is open directly by the browser and not by javascript – Ketchup Oct 30 '20 at 09:34
  • maybe there is a misunderstanding - for a window to be closed by javascript, **the window needs to have been opened by javascript**. does that make it clearer? – berlin Oct 30 '20 at 09:37
  • Yes I get it but the browser's find can't be close on this way also because it is not always the active window even if it is open – Ketchup Oct 30 '20 at 10:25
  • just had a relook at the question - am clear on what you are trying to do now - i would say it is not possible. for more information, do check out [this page](https://stackoverflow.com/a/6680309/13448177). you can only detect when a user has pressed the actual ctrl+f key. – berlin Oct 30 '20 at 10:42