I have a simple chrome extension that sends a notification (https://developer.chrome.com/docs/extensions/reference/notifications/) from the extension's background script. Upon clicking on the notification, a content script is executed via the chrome.tabs.query function using a url match. Inside the script I've been trying everything to focus the tab it's running on, with no success.
background.js:
const focusTab = () => {
chrome.tabs.query({ url: 'myUrlMatch'}, tabs => {
chrome.tabs.executeScript(tabs[0].id, { file: 'focustab.js' });
});
}
// I'm calling focusTab from the chrome.notifications.onClicked listener callback. From the click until inside the below f() function everything works.
focustab.js:
function f() {
window.focus();
window.parent.focus();
window.parent.parent.focus(); //just to make sure
}
setTimeout(f, 0); //superstition coming from angularJS
I cannot believe it's impossible to focus the tab a notification is originating from. I've read conflicting things about the permissions one has when it comes to navigating with windows and tabs in modern browsers. I'm reading a lot about window.focus() but nothing seems to be happening at all when I try playing around with that in chrome.
This is a fairly logical interaction in my eyes, to expect a notification click to take you to the tab it's coming from. Just how it works with iOS push notifications.
What am I doing wrong to get something so trivial not to work? I've read that due to security concerns browsers don't allow you to focus/switch a tab etc. I understand this makes sense in the general context of running js code, but in this specific context of clicking a notification, we clearly have a type of user consent and intention here, we even have a specific 'notifications' permission in the extension's manifest file. This must be possible.
Any help would be appreciated