The*_*lob 8 google-chrome javascript-events
我已经构建了一个使用window.print()启用打印的页面.由于管理层有一些非常不寻常的要求,我需要能够捕获调用window.print()时出现的打印菜单的click事件.具体来说,在Chrome中,我需要捕获"打印"(蓝色)和"取消"(灰色)按钮的点击事件.
我不得不承认我甚至不知道从哪里开始.我检查了每个元素,可以看到这些是标准的html元素.这些按钮具有类(打印默认的打印按钮和取消的取消按钮),但没有标识.
我还注意到在打印菜单之外没有可见的DOM,并且打印菜单html标签的ID为'print-preview'.
如何捕获打印菜单按钮的点击事件(至少在Chrome中)?
小智 6
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');
}
});
Run Code Online (Sandbox Code Playgroud)
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.
| 归档时间: |
|
| 查看次数: |
5141 次 |
| 最近记录: |