afterprint event fires immediately after beforeprint, even before print dialog is closed
Categories
(Core :: Layout, defect)
Tracking
()
People
(Reporter: ap3x91, Unassigned)
Details
User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:130.0) Gecko/20100101 Firefox/130.0
Steps to reproduce:
TL;DR:
The window.onafterprint event handler fires immediately when opening the print preview dialog. So the window.onbeforeprint and window.onafterprint events are both immediately triggered after each other. The window.onafterprint event should be waiting for the printing to happen or the dialog to be closed / cancelled. It works fine in Chromium.
From the MDN page Window: afterprint event:
"The afterprint event is fired after the associated document has started printing or the print preview has been closed."
System:
Linux (Pop!_OS 22.04 LTS)
Kernel version: 6.9.3-76060903-generic
Firefox 130.0
X11
GNOME 42.9
More details / use case:
As far as I'm aware there's no direct way to set a default file name for saving a page as PDF via the print dialog - the default file name will just be the HTML page's document.title (maybe sanitized to get rid of characters that might be "illegal" in certain file systems). So I wanted to keep a concise and "pretty" title in the browser tab, while still having a good default file name for my use case. So I wanted to use a window.onbeforeprint event handler to change the document.title before the print process, so that I can conveniently use the default file name and then revert back to the old "pretty" document.title in a window.onafterprint event handler.
I've tested this approach in Chromium and it worked like a charm, so I would assume that this should work just the same in Firefox. I couldn't find anything that would suggest otherwise on the MDN regarding onbeforeprint.
Here's a small example HTML:
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Test</title>
</head>
<body>
Hello World.
</body>
<script>
const normalTitle = document.title;
window.onbeforeprint = function(event) {
document.title = "Filename_Test";
console.log("before print fired")
}
window.onafterprint = function() {
document.title = normalTitle;
console.log("after print fired")
}
</script>
</html>
Actual results:
With the example HTML from above:
- open print dialog / preview with
CTRL + P - check the browser console and find logs from the
afterprintevent handler even though neither actual printing has started nor has the print preview been cancelled / closed otherwise
Regarding the actual use case that led to me finding this issue:
- Open print dialog / preview with
CTRL + P - Tab title in the background didn't change (because it got immediately reverted)
- In the browser console the logs for both
onbeforeprintandonafterprintevents are already present - Choosing "Save as PDF" and clicking the "Save" button still provides the default file name "Test.pdf" instead of "Filename_Test.pdf" which should have been set in the
onbeforeprinthandler
Expected results:
When opening the print dialog / preview via CTRL + P only the window.onbeforeprint event should be fired. The window.onafterprint event should only be fired when the printing process has been successfully started and the preview dialog is closed or otherwise cancelled.
Comment 1•2 years ago
|
||
The Bugbug bot thinks this bug should belong to the 'Core::Widget: Gtk' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.
I just confirmed today that the scenario described above behaves the same on a Windows 10 PC.
Updated•2 years ago
|
Comment 3•2 years ago
|
||
See discussion in bug 1663290
Description
•