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!
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.