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

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

问题描述

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



这里是:

如果您尝试在一个单一的内部频繁调用 window.print()方法在Chrome浏览器中,浏览器会抛出一条警告消息,指出:


忽略过于频繁的打印()


没有任何反应!几秒钟后,事情将恢复正常,并且在您再次调用 window.print()命令时出现打印对话框!更糟糕的是,好的Chrome浏览器人员对调用print命令的页面使用指数等待时间,这意味着用户点击按钮的次数越多,他就必须等待打印对话框出现!



这个问题已经在很长一段时间(14个后续版本)中被查看,并且已经确认 Area-UI bug,我为谷歌团队再次发布昨天希望如果有人来自Chrome团队可以验证这个令人难以置信的烦人功能何时将被修复!



然而,我在这里寻找的是解决方法问题,是否有什么我可以做到这一点?我的公司正在开发一个高度交易性的金融系统,其中包含大量需要打印的报告,并且就这一点小故障而言,整个项目有可能在我最喜欢的谷歌Chrome浏览器中运行!



更新:



这里是Chrome浏览器中的代码,它引起了这个特性,看起来至少需要2秒才能再次调用print命令,所以一个2秒钟间隔的定时器UI可能会阻止进入无限的等待回调!任何其他的想法?

解决方案

您可以有条件地替换 window.print() function:

  //检测浏览器是否为Chrome 
if(navigator.userAgent.toLowerCase()。indexOf (chrome)> -1){
//在封闭中封装私有变量
(function(){
var realPrintFunc = window.print;
var interval = 2500; // 2.5秒
var nextAvailableTime = + new Date(); //当我们可以再次安全地打印时

//覆盖window.print函数
window.print =函数(){
var now = + new Date();
//如果下一个可用时间在过去,现在打印
if(now> nextAvailableTime){
realPrintFunc();
nextAvailableTime = now + interval;
} else {
//下次可用时打印
setTimeout(realPrintFunc,nextA vailableTime - now);
nextAvailableTime + = interval;
}
}
})();

$ / code>

不使用外部安全定时器/包装,您可以使用内部安全定时器。只需添加这个和 window.print 在Chrome和其他地方安全地运行。 (另外,关闭意味着 realPrintFunc interval nextAvailableTime 对于新的 window.print



如果您需要将呼叫协调到窗口.print ,你可以在父页面而不是在闭包中设置 nextAvailableTime ,这样所有的框架都可以访问共享价值使用 window.parent.nextAvailableTime


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!

Here's it is:

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:

Ignoring too frequent calls to print()

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!

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!

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!

Update:

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?

解决方案

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;
            }
        }
    })();
}

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

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天全站免登陆