如何捕获JavaScript调用的默认打印菜单上的click事件window.print()? [英] How to capture the click event on the default print menu called by Javascript window.print()?

查看:2165
本文介绍了如何捕获JavaScript调用的默认打印菜单上的click事件window.print()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用window.print()构建了一个可打印的页面。由于管理的一些非常不寻常的要求,我需要能够捕获window.print()被调用时出现的打印菜单的点击事件。具体来说,在Chrome中,我需要捕获打印(蓝色)和取消(灰色)按钮的点击事件。

我不得不承认我不会甚至不知道从哪里开始。我检查了每个元素,并可以看到这些是标准的html元素。这些按钮具有类别(打印按钮的打印默认和取消按钮的取消),但没有ID。

我还注意到,在打印菜单之外没有可见的DOM,并且打印菜单html标签的ID为'print-preview'。



如何捕获打印菜单按钮的点击事件(至少在Chrome中)?

解决方案

您无法直接从常规网页访问Chrome的内部窗口(在这种情况下,打印对话框)。



确定打印对话框为打开或关闭,您可以捕获matchMedia事件(仅限webkit):

  var mediaQueryList = window.matchMedia('print'); 
mediaQueryList.addListener(function(mql){
if(mql.matches){
console.log('print print open'';
} else {
console.log('print print dialog closed');
}
});

但是您无法检查是否点击了打印或取消按钮。此信息无法从常规网页访问。



要获取有关Chrome打印机作业的信息,您只能为Chrome创建扩展程序并收听 onPrintRequested事件。当然,扩展程序必须安装到每个页面用户的浏览器中。


I have built a page that is print-enabled using window.print(). Because of some really unusual requirements from management, I need to be able to capture the click event for the print menu that appears when window.print() is called. Specifically, in Chrome, I need to capture the click event for the 'Print' (blue) and 'Cancel' (gray) buttons.

I have to admit I don't even know where to start here. I inspected each element and can see that these are standard html elements. These buttons have classes (print default for the print button and cancel for the cancel button) but no IDs.

I also noticed that no DOM is visible beyond the print menu, and the print menu html tag has an ID of 'print-preview'.

How do I capture click events for the print menu buttons (in Chrome at least)?

解决方案

You can not access Chrome's internal windows (printing dialog in this case) directtly from a regular web page.

To determine that printing dialog was opened or closed you can catch matchMedia events (webkit only):

var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
    if (mql.matches) {
        console.log('before print dialog open');
    } else {
        console.log('after print dialog closed');
    }
});

But you can not check if 'Print' or 'Cancel' button was clicked. This information is not accessible from regular web page.

To get information about Chrome's printer jobs you can only create extension for Chrome and listen onPrintRequested event in content script. Of course extension must be installed into browser of each page user.

这篇关于如何捕获JavaScript调用的默认打印菜单上的click事件window.print()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆