Chrome 阻止了 JavaScript 打印,解决方法? [英] JavaScript print blocked by Chrome, Workaround?

查看:92
本文介绍了Chrome 阻止了 JavaScript 打印,解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里之前已经讨论过,但我没有找到实际的解决方案/解决方法,我希望有人知道如何解决这个问题!

I know it has been discussed here before, yet I found no practical solution/workaround for this, I'm hoping if someone has any idea how to resolve this problem!

这是:

如果您尝试在 google Chrome 中的单个页面内频繁调用 window.print() 方法(就像用户单击打印按钮一样),浏览器会在控制台中抛出警告消息, 说明:

If you try to call window.print() method frequently within a single page(as if a user clicks on a print button) in google Chrome, the browser throws a warning message in the console, stating:

忽略对print()过于频繁的调用

Ignoring too frequent calls to print()

什么也没发生!几秒钟后,事情恢复正常并在您再次调用 window.print() 命令时出现打印对话框!更糟糕的是,优秀的 Chrome 开发人员对调用打印命令的页面使用指数级等待时间,这意味着用户点击按钮进行打印的次数越多,他等待打印对话框出现的时间就越长!

And nothing happens! After several seconds, things go back to normal and print dialog appears the moment you call window.print() command again! To make matters worse, the good Chrome folks use exponential wait time for a page that calls print command, meaning the more user clicks on a button to print, the more he has to wait for the print dialog to appear!

这个问题在 chrome 中已经存在了很长时间(14 个后续版本)并且确认是一个 Area-UI 错误,我昨天再次为谷歌团队发布,希望 Chrome 团队的某个人可以验证何时这令人难以置信烦人的功能将得到修复!

This issue has been in chrome for quite some time (14 subsequent versions) and it is confirmed as being an Area-UI bug, I posted it again for google team yesterday hoping if someone from Chrome team can verify when this incredible annoying feature is going to be fixed!

但是,我在这里寻找的是解决此问题的方法,有没有任何我能做的事情来解决这个问题?我的公司正在开发一个高度交易的财务系统,其中有很多需要打印的报告,仅此一个小故障,整个项目就有在我最喜欢的谷歌 Chrome 浏览器中运行的风险!

However, what I'm looking for here is a workaround for this problem, is there anything I can do be able to get this working? My company is developing a highly transactional financial system with lots of reports that needs printing, and for just this one little glitch, the whole project is at risk of running in my favorite google Chrome browser!

更新:

这是 Chrome 浏览器中的代码 导致此功能的原因似乎至少需要 2 秒才能有人再次调用打印命令,因此 UI 中间隔为 2 秒的计时器可能会阻止进入无限等待回调!还有其他想法吗?

Here's the code in Chrome browser that causes this feature and it looks like that at least 2 seconds is needed before someone calls print command again, so a timer of 2 seconds interval in UI could possibly prevent getting into an infinite wait callback! any other thoughts?

推荐答案

你可以有条件地替换 window.print() 函数:

You could conditionally replace the window.print() function:

// detect if browser is Chrome
if(navigator.userAgent.toLowerCase().indexOf("chrome") >  -1) {
    // wrap private vars in a closure
    (function() {
        var realPrintFunc = window.print;
        var interval = 2500; // 2.5 secs
        var nextAvailableTime = +new Date(); // when we can safely print again

        // overwrite window.print function
        window.print = function() {
            var now = +new Date();
            // if the next available time is in the past, print now
            if(now > nextAvailableTime) {
                realPrintFunc();
                nextAvailableTime = now + interval;
            } else {
                // print when next available
                setTimeout(realPrintFunc, nextAvailableTime - now);
                nextAvailableTime += interval;
            }
        }
    })();
}

您可以使用内部的而不是使用外部安全计时器/包装器.只需添加这个,window.print 在 Chrome 和其他任何地方都可以安全运行.(另外,闭包意味着 realPrintFuncintervalnextAvailableTime 对新的 window.print

Instead of using an external safety timer/wrapper, you can use an internal one. Just add this and window.print behaves safely in Chrome and normally everywhere else. (Plus, the closure means realPrintFunc, interval and nextAvailableTime are private to the new window.print

如果您需要在多个框架页面之间协调对 window.print 的调用,您可以在父页面中设置 nextAvailableTime,而不是在闭包中,以便所有框架可以使用 window.parent.nextAvailableTime 访问共享值.

If you need to coordinate calls to window.print between multiple framed pages, you could set nextAvailableTime in the parent page, rather than in the closure, so that all the frames could access a shared value using window.parent.nextAvailableTime.

这篇关于Chrome 阻止了 JavaScript 打印,解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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