Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to inform the user of errors and progress from a Service Worker? #259

Closed
fregante opened this issue Aug 20, 2022 · 7 comments
Closed
Labels
discussion Needs further discussion topic: service worker Related to service worker background scripts

Comments

@fregante
Copy link

I'm reposting this unresolved request from another now-archived repo.


Until now, when executing code from the browser_action or context_menu (in background.js), you could either inject code into the current page or use alert to communicate with the user.

The first option is already limited since it only works if the extension has access to the tab and the second option already does not work in Firefox and Safari.

Do you have any alternatives for this since Service Workers don't have alert() at all?

@fregante
Copy link
Author

Opening a custom window via the Windows API seems nice:

Similar code (but adapted to use the Windows API instead):

var myWindow = window.open("", "", "width=500,height=150");
myWindow.document.write('<span>Huston we got a problem <button>Okay')
myWindow.document.body.style = 'justify-content: center;display: flex;align-items: center;'
// + centering

But there's no way to add a script to enable an "Ok" button:

  • data: URLs aren't supported
  • tabs.executeScript doesn't have access to about:blank
  • tabs.executeScript doesn't have access to chrome-extension:// pages

It seems that the only way to show custom content in a window is to load an HTML file that exists in the extension itself (or remote URL with permission).

It seems that the minimum possible solution to this requires:

  • message.js with button.onclick = () => window.close() + message.textContent = location.search
  • message.html to load message.js

Is there any other way to achieve my example above without creating these files?

@xeenon xeenon added the topic: service worker Related to service worker background scripts label Aug 31, 2022
@tophf
Copy link

tophf commented Sep 12, 2022

You can open a new window using chrome.windows.create({url: 'alert.html?' + new URLSearchParams({data: 'foo'}), type: 'popup'}) then alert.html will build the UI and send the result back by messaging.

@fregante
Copy link
Author

I already described that solution:

  • message.html to load message.js

Having to build and manage such a core functionality when alert already exist(ed) feels like a step back.

@xeenon
Copy link
Collaborator

xeenon commented Sep 12, 2022

Seems like we should consider an extension alert API.

@xeenon xeenon added agenda Discuss in future meetings discussion Needs further discussion labels Sep 12, 2022
@dotproto
Copy link
Member

Opening a custom window via the Windows API seems nice:

That's the path I would advise. As you described, the default extension CSP prevents developers from declaring the entire page inline. At the moment to implement this in a Chrome extension a developer would need to have static HTML and JS files.

Having to build and manage such a core functionality when alert already exist(ed) feels like a step back.

I think that's a fair criticism. alert() and prompt() certainly have their problems (namely blocking JS execution) but they have a lot of utility. It's possible to implement workarounds, I agree that it feels like not all batteries are included in the box any more.

@dotproto
Copy link
Member

@fregante, while working on something else I just put together a quick proof of concept for an alert-like popup. Note that this window will have a null origin.

let msg = `Well, that's one way to show an alert`;
let url = `data:text/html,\
<!DOCTYPE html>
<html>
<head>
    <!-- If you don't specify a title, the data URL will appear in the title bar -->
    <title>Alert Demo</title>
</head>
<body>
  <p>${msg}</p>
  <button id="close-button">Close</button>

  <script>
    const closeButton = document.getElementById('close-button');
    
    // Close the popup if the user clicks the "close" button
    closeButton.addEventListener('click', _ => window.close());
    
    // Automatically close this popup if it loses focus
    window.addEventListener('blur', _ => window.close());
  </script>
</body>
</html>`
chrome.windows.create({url, type: "popup", height: 200, width: 300})

@dotproto dotproto removed the agenda Discuss in future meetings label Mar 16, 2023
@fregante
Copy link
Author

fregante commented Jan 20, 2024

@dotproto it works in Chrome and Safari, but Firefox won't accept data URLs. 😰 Kinda disappointing because Firefox has never allowed alert in the background :(

I finally managed to publish a module that makes use of that technique as a fallback to alert:

Screenshot 4

For now I'll close this issue as there's a proposal for a new API to resolve this:

If you have further ideas that might work on Firefox I'm all ears.

@fregante fregante closed this as not planned Won't fix, can't repro, duplicate, stale Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Needs further discussion topic: service worker Related to service worker background scripts
Projects
None yet
Development

No branches or pull requests

4 participants