Can I tell if the popup was opened with mouse click or command?

335 views
Skip to first unread message

Juraj M.

unread,
Dec 1, 2023, 5:34:37 PM12/1/23
to Chromium Extensions
In my manifest I have this:
"commands": {
   "_execute_action": {
     "suggested_key": {
       "default": "Alt+O"
     }
  }
}
Is it possible to tell (in the popup code) if the popup was opened using the command or the mouse click?

Patrick Kettner

unread,
Dec 6, 2023, 5:58:06 AM12/6/23
to Juraj M., Chromium Extensions
That is not currently possible. Out of curiosity, why would you be interested in that information?

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/c85a406c-1492-46c4-aef5-0110fa7bf6aen%40chromium.org.

Juraj M.

unread,
Dec 6, 2023, 6:40:09 AM12/6/23
to Chromium Extensions, Patrick Kettner, Chromium Extensions, Juraj M.
Some users wants the toolbar icon to open my extension page (instead of opening it's popup window). So the while opening the popup I check the settings and if needed, I close the popup before it fully opens and open new tab.
So I was hoping I could allow these users to still open the popup window using the keyboard shortcut.
But I can think of two workarounds - 1. I can detect "Alt" key being pressed while the popup code starts. 2. I can change "_execute_action" into normal command and handle in manually in the code... wait a second, actually I can't open popup from the code yet, right? :(, ok so one workaround available :)

Jackie Han

unread,
Dec 6, 2023, 7:58:57 AM12/6/23
to Juraj M., Chromium Extensions, Patrick Kettner
I give users a setting for popup mode or tab mode.
When in popup mode, clicking the icon or using "_execute_action" shortcut opens the popup.
When in tab mode, clicking the icon or using "_execute_action" shortcut opens the popup page in a tab (or active the existing page).

Jackie Han

unread,
Dec 6, 2023, 8:00:26 AM12/6/23
to Juraj M., Chromium Extensions, Patrick Kettner
In popup mode, I also give users a button to open it in a new tab.

Juraj M.

unread,
Dec 6, 2023, 8:09:45 AM12/6/23
to Chromium Extensions, Jackie Han, Chromium Extensions, Patrick Kettner, Juraj M.
Thanks Jackie, but I have no idea what you mean :), how do you change what "_execute_action" does, isn't it fixed once you set it in manifest? And it doesn't even fire onCommand event based on the docs:

The _execute_action (Manifest V3), _execute_browser_action (Manifest V2), and _execute_page_action (Manifest V2) commands are reserved for the action of trigger your action, browser action, or page action respectively. These commands do not dispatch command.onCommand events like standard commands.

If you need to take action based on your popup opening, consider listening for a DOMContentLoaded event inside your popup's JavaScript.

Jackie Han

unread,
Dec 6, 2023, 8:21:37 AM12/6/23
to Juraj M., Chromium Extensions, Patrick Kettner
You can change these two modes dynamically.

// manifest.json
  "action": {
    "default_popup": "popup.html"
  },
  "commands": {
    "_execute_action": {
    }
  },


// user-settings.js
    if (setting === 'TabMode') {
      chrome.action.setPopup({popup: ''}); // now chrome.action.onClicked will work
    } else { // popup mode
      chrome.action.setPopup({popup: 'popup.html'}); // popup mode will work
    }

// background.js
chrome.action.onClicked.addListener(function() {
    openPageInTab();
});

Juraj M.

unread,
Dec 6, 2023, 8:27:57 AM12/6/23
to Chromium Extensions, Jackie Han, Chromium Extensions, Patrick Kettner, Juraj M.
Thank you Jackie!
I didn't know about this behavior, and it seems to be nicely documented too!
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/browserAction/setPopup

Jackie Han

unread,
Dec 6, 2023, 9:06:08 AM12/6/23
to Juraj M., Chromium Extensions, Patrick Kettner
You are welcome.

To keep action.setPopup() behavior across the browser/extension restart/update, you should also set it like below

function init() {
   let setting = readFromStorage();
   action.setPopup(a-value-depends-on-setting) ;
}
chrome.runtime.onInstalled.addListener(init);
chrome.runtime.onStartup.addListener(init); 

Juraj M.

unread,
Dec 6, 2023, 9:14:16 AM12/6/23
to Chromium Extensions, Jackie Han, Chromium Extensions, Patrick Kettner, Juraj M.
Yeah, this edge cases handling is pretty terrible :). You forgot to mention the extension disable/enable case!
I'm actually using session storage to detect that the extension code is running "for the first time" and do all initialization there.
This is my helper for that:

const KEY = '_ext_started';

// resolves to "TRUE" only if extension just booted. Resolves to "FALSE" if service worker just woke up OR if called from non-background script.
export const didExtensionStarted = (async () => {
  // only allowed in background:
  if (!isRunningInBackground) return false;
  // in MV3 we check runtime storage
  const {[KEY]: extensionStarted} = await browser.storage.session.get(KEY);
  if (extensionStarted) return false;
  // we save the time when extension started
  await browser.storage.session.set({[KEY]: Date.now()});
  return true;
})();

Juraj M.

unread,
Dec 6, 2023, 9:15:26 AM12/6/23
to Chromium Extensions, Juraj M., Jackie Han, Chromium Extensions, Patrick Kettner
I meant it in general, not the code you wrote :)

Oliver Dunk

unread,
Dec 6, 2023, 9:16:08 AM12/6/23
to Juraj M., Chromium Extensions, Jackie Han, Patrick Kettner
We've discussed a runtime.onExtensionLoaded event in the Web Extensions Community Group which I hope could help here in the future: https://github.com/w3c/webextensions/issues/353#issuecomment-1593591201

Not a short term solution but I definitely appreciate the problem :)
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB


Juraj M.

unread,
Dec 6, 2023, 9:25:39 AM12/6/23
to Chromium Extensions, Oliver Dunk, Chromium Extensions, Jackie Han, Patrick Kettner, Juraj M.
Thank you Oliver, that will be a great help! :)
Reply all
Reply to author
Forward
0 new messages