w3c / webextensions

Charter and administrivia for the WebExtensions Community Group (WECG)
Other
603 stars 56 forks source link

Proposal: onDoubleClicked for `browserAction` #196

Closed stefanvd closed 2 years ago

stefanvd commented 2 years ago

Motivation

The use of Manifest V2 to Manifest V3 diminishes the good web experience. And create some inconvenience for existing users using the DoubleClick action on my browser extensions.

The currently available listener for a click: browser.browserAction.onClicked.addListener(listener)

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/browserAction/onClicked

Due to the lack of a support timer (setTimeout and setInterval) in the service worker, there is no way to detect the user double-clicking. Even with the chrome.alarms.create("MyDelay", {when: Date.now() + 1 * 3000}) you can get it to work in seconds. However, the documentation said once you have published the Chrome extension, the actual interval will be rounded up to 1 minute even if you set a smaller value like 3 seconds (3000ms). Source: https://developer.chrome.com/docs/extensions/reference/alarms/#method-create

The following browser extensions use the double click function:

Proposal

browser.browserAction.onDoubleClicked.addListener(listener) or allow the chrome.alarms.create("MyDelay", {when: Date.now() + 1 * 3000}) for seconds.

It will extend the user experience for desktop and mobile web browsers. And create a convenient way for mobile users to get more control on a small device. Such as opening the options page of that browser extension.

stefanvd commented 2 years ago

Crbug report https://bugs.chromium.org/p/chromium/issues/detail?id=1315555

dotproto commented 2 years ago

Due to the lack of a support timer (setTimeout and setInterval) in the service worker, there is no way to detect the user double-clicking.

setTimeout and setInterval are exposed in workers, but they are only valid for the duration of that worker. As with any other JS context, all timers created in a worker context are cleared when that context is terminated. As such, there are many situations where JS timers may not behave as expected, but I don't think this is one of them. The timeout for a double click handler is short enough that setTimeout still works as expected.

Here's a functional demo of a double click action in an MV3 extension that's heavily cribbed from Turn Off the Lights.

stefanvd commented 2 years ago

That is my code (see my Github profile icon). That is conflicting information, because according to this article it is recommended to move away from setInterval.

Moving from timers to alarms It's common for web developers to perform delayed or periodic operations using the setTimeout or setInterval methods. These APIs can fail in service workers,

Source: https://developer.chrome.com/docs/extensions/mv3/migrating_to_service_workers/#alarms

I did move to chrome.alarms.onAlarm.addListener API. And tested the development unpacked Chrome extension: https://github.com/turnoffthelights/Turn-Off-the-Lights-Chrome-extension/blob/manifest-v3/src/js/background.js#L290 However, in my research, this code will not work in the Chrome extension package version according to that article. Once you've published the Chrome extension, the actual interval will be rounded up to 1 minute even if you set a smaller value like 250 milliseconds (0.004166 minutes).

Thank you. It is good to hear from you @dotproto that the current manifest v2 doubleclick code will not affect this issue for the service worker manifest v3. However, the suggestion of the proposal is open to further shaping the browser extension ecosystem. That is easy for users and developers.

Rob--W commented 2 years ago

This was discussed during today's WECG meeting. The meeting notes are in the linked PR and will be merged with the repo within two weeks.

Stefan - it seems that you're primarily interested in a new API because of the belief that setTimeout cannot be relied on. That is not correct. While it's true that Service workers (and event pages) can shut down at inconvenient times, any reasonable implementation would not do so shortly after the user clicks the extension button. In other words, setTimeout with a small delay should work reliably.

stefanvd commented 2 years ago

The proposal for the new API would be useful to reduce the code size. And add some new capabilities to improve the web experience. For example. On mobile, you have 'single tap', 'long press', but no right-click. And the new 'double tap' which can create a new way for different functionality. Such as here on my browser extension, double-tap the lamp button, which opens a mini settings panel.

As I mentioned before, it is good to hear that the smaller setTimeout won't break this double click function in the manifest v3 service worker.

Rob--W commented 2 years ago

I'm closing this issue because we have discussed this topic and decided to not implement the requested method, as explained at the "Proposal: onDoubleClicked for browserAction" section of https://github.com/w3c/webextensions/blob/main/_minutes/2022-04-14-wecg.md.

While "Long press" could potentially be an alternative on mobile to double-click on desktop, it does not address the mentioned "affordance" issue, i.e. there not being a way for the user to know that long-press/double-click doing anything.