Javascript window.open可在Chrome控制台中使用,但不能作为摘要 [英] Javascript window.open works in Chrome console but not as a snippet

查看:177
本文介绍了Javascript window.open可在Chrome控制台中使用,但不能作为摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,我正在研究一些小的脚本来修改经常使用的网页.我要自动执行的操作之一是在标签中打开某些链接,因此不必手动进行操作.

I'm relatively new to Javascript and I'm working on some little scripts to modify webpages that I work with often. One of the things I'm automating is opening certain links in tabs and so I don't have to do so manually.

如果我将此行放入代码段并运行它,则不会发生任何事情:

If I put this line into my snippet and run it, nothing happens:

window.open("http://www.google.com");

但是,如果我在控制台中键入它并按回车键,它将打开一个新选项卡,其URL就很好.所有这些都是在检查器中发生的.为什么在一个地方却不能在另一个地方起作用?有解决方法吗?

But if I type it into the console and hit return, it opens a new tab with the url just fine. All of this is happening in the Inspector. Why does it work in one place but not the other? Is there a workaround?

推荐答案

Chrome浏览器检测到不是来自用户手势的对window.open()的调用时,会将其阻止为弹出窗口.

When Chrome detects a call to window.open() which does not originate from a user gesture, it blocks it as a pop-up.

您可以通过显示在网址栏右边缘的弹出式小窗口模式手动覆盖此设置:

You can manually override this via the little Pop-Up modal that shows up on the right-edge of your URL bar:

您提出的有趣的事情是:为什么它可以在控制台中运行,但不能在代码片段中运行?我询问了DevTools团队,其中一个非正式地认为DevTools将控制台执行视为用户手势,以避免此类安全检查.

The interesting thing you brought up is: why does it work from the Console, but not from Snippets? I asked the DevTools team, and one of them informally thinks that DevTools treats Console execution as user gestures, to avoid security checks like these.

一种可能对您足够好的解决方法是从新的Chrome标签(chrome://newtab)运行代码段.当我从该页面运行代码段时,可以打开新窗口而无需触发弹出窗口阻止"模式.

One workaround that may be good enough for you is to run your snippet from a new Chrome tab (chrome://newtab). When I run the Snippet from that page, I can open new windows without triggering the "Pop-up blocked" modal.

感谢您在控制台和代码段的工作方式之间进行实验.事实证明这很有趣.

Thanks for experimenting between how it worked with Console and Snippets. That turned out to be pretty interesting.

这篇关于Javascript window.open可在Chrome控制台中使用,但不能作为摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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