简体   繁体   English

使用Chrome在window.open中更改行为

[英]Behaviour change in window.open using Chrome

I have the following bookmarklet which opens all posts shown in the "Unread topics" page of a SMF forum (the original is a single line): 我有以下书签,它打开了SMF论坛的“未读主题”页面中显示的所有帖子(原文为单行):

javascript:(function(){
  var topics = new Array();
  var links = document.getElementsByTagName('a');
  for (var i = 0; i < links.length; i++) {
    if (links[i].href.indexOf('#new') != -1)
      topics.push(links[i].href);
  }
  for (var i = 0; i < topics.length; i++) {
    window.open(topics[i]);
  }
})();

It used to work fine: I opened the page with the links, clicked the bookmarklet, and all the topics would open in new tabs. 过去工作正常:我打开了带有链接的页面,单击了书签,所有主题都将在新选项卡中打开。

After the last Chrome update (my current version is 22.0.1229.79 m, Windows 7 x64), this changed. 在最近一次Chrome更新(我的当前版本是22.0.1229.79 m,Windows 7 x64)之后,此更改发生了变化。 When I click it, the first topic is opened in a new tab, but the other topics open in popups (I had to authorize them the first time). 当我单击它时,第一个主题在新选项卡中打开,而其他主题在弹出窗口中打开(我必须第一次授权它们)。

My questions are: 我的问题是:

  1. How to workaround this issue and restore old behaviour? 如何解决此问题并恢复旧的行为?
  2. What caused this change? 是什么引起了这种变化?

Open a new tab in the background? 在后台打开新标签页? might help: the answer there recommends using document.createEvent and then initMouseEvent to simulate a ctrl-click - or a meta-click on OS X. 可能会有所帮助:答案建议使用document.createEvent ,然后使用initMouseEvent来模拟ctrl单击-或在OS X上进行meta单击。

Try this hack as a work around. 尝试使用此技巧。 Worked for me. 为我工作。

javascript:(function(){
  var topics = new Array();
  var links = document.getElementsByTagName('a');
  for (var i = 0; i < links.length; i++) {
    if (links[i].href.indexOf('#new') != -1)
      topics.push(links[i].href);
  }
  for (var i = 0; i < topics.length; i++) {
    setTimeout(function(){ window.open(topics[i]) }, 500); // ADDED setTimeout
  }
})();

You may need to modify Chrome's popup block settings. 您可能需要修改Chrome的弹出窗口阻止设置。 If chrome blocks popups, a small icon will appear in the right corner of the URL bar. 如果Chrome阻止弹出窗口,则网址栏的右上角会出现一个小图标。

You can also edit them directly here: chrome://chrome/settings/contentExceptions#popups 您也可以在此处直接编辑它们:chrome:// chrome / settings / contentExceptions#popups

I was able to open at least 30 tabs this way, but there seems to possibly be a limit on the number of tabs that can be opened this way. 我至少可以通过这种方式打开30个标签,但是似乎可以通过这种方式打开的标签数量有所限制。 Playing around with the timeout may help. 玩超时可能会有所帮助。 Or you can open 30, then click again to open 30 more, etc - with a minor code change, obviously. 或者,您可以打开30,然后再次单击以打开30个以上,依此类推-显然,只需更改一下代码即可。 "30" is just an example. “ 30”仅是示例。 Experiment to find an optimum number. 尝试找到最佳数字。

It is worth noting that when I modified the popup blocker settings and used code like yours without setTimeout, what happened is that 2 tabs opened, and then a huge number of new windows opened. 值得一提的是,当我修改了弹出窗口阻止程序设置并使用了没有setTimeout的类似您的代码时,发生的事情是打开了两个选项卡,然后打开了大量新窗口。 I consider this to be bug. 我认为这是错误。 I can't see any possible reason to change from tabs to windows under this scenario. 在这种情况下,我看不出有任何可能的原因从选项卡更改为窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM