0

I'm writing this piece of code to allow me to quickly search for the highlighted text on a webpage using a firefox plugin. I have the code publicly hosted here.

My function to capture the keypress ctrl+s and do the search is the following:

document.addEventListener("keydown", function(e) {
// Help came from https://stackoverflow.com/a/14562869/6897392
 if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)){
   text = getSelectionText();
   if(text != ""){
      e.stopImmediatePropagation();
      e.preventDefault();
      console.log(text);
      openInNewTab(searches[default_search]+text);
   }
 }
}, false);

If I comment out the openInNewTab(searches[default_search]+text); line, which is the following function:

function openInNewTab(url) {
// Help came from https://stackoverflow.com/a/11384018/6897392
  var win = window.open(url, '_blank');
  win.focus();
  return false;
}

It will prevent the save dialogue. However, if I leave the code in, it will prevent the save dialog in the original tab, but the new tab that it opens will pop up the save dialogue.

I have had no luck figuring out how to prevent the save dialogue from appearing in the second window, and would like some help.

Thank you!

Al Fahad
  • 2,378
  • 5
  • 28
  • 37

1 Answers1

1

If it's really doing what you describe, that sounds like a bug in Firefox, but you should be able to work around it by delaying your openInNewTab call very briefly:

document.addEventListener("keydown", function(e) {
// Help came from https://stackoverflow.com/a/14562869/6897392
 if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)){
   text = getSelectionText();
   if(text != ""){
      e.stopImmediatePropagation();
      e.preventDefault();
      console.log(text);
      setTimeout(() => {                                // ***
          openInNewTab(searches[default_search]+text);
      }, 50);                                           // ***
   }
 }
}, false);

Firefox's popup blocker should allow it, because it's scheduled within a user-initiated event.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I'll have to give it a shot! I attempted to do the set timeout before, but my syntax looked different from yours. If that does not work and it does seem to be a bug, I'll contact Mozilla about it. I appreciate your help! – William McCall Nov 04 '18 at 21:16
  • I just changed it to your suggestion and it works perfectly. I'm setting your comment as the answer; thank you again! – William McCall Nov 04 '18 at 21:19
  • 1
    @WilliamMcCall - No worries! :-) I would narrow it down to an MCVE and report it as a bug to Mozilla. – T.J. Crowder Nov 05 '18 at 07:19