3

如果我启动一个包含 Flash 游戏的窗口,并且 Chrome 的弹出窗口阻止程序将其抑制,则用户会听到游戏中的音乐,但什么也看不到。这是因为 Chrome 不会阻止窗口;它只是隐藏它。

Chrome 加载页面以获取网站图标和页面标题,因此他们可以在弹出窗口阻止程序对话框中显示它(单击地址栏中的图标时会看到该对话框)。但是他们并没有关上窗户。插件运行,脚本可以回调打开器 - 它就像一个常规窗口,但不可见。(好的,恶意软件开发人员,请停止流口水。)

Chrome 可以解析 <head> 标记的内容以检索 <title> 和 favicon <link/> 标记,而无需实际加载页面。他们可以在检索到图标/标题后关闭隐藏窗口。他们可以只显示 URL 而不是图标/标题。相反,该错误自 2008 年以来继续恶化……

http://code.google.com/p/chromium/issues/detail?id=3477

http://code.google.com/p/chromium/issues/detail?id=38458

无论如何,问题是:我能检测到这种情况何时发生吗? 也许 window.chrome JavaScript 对象中有一些东西表明窗口处于这种不可见状态,所以我可以关闭它或移除闪光灯?

__

根据 Prusse 的回答,这就是我最终得到的结果......

function isPopupZombie(popWindow, delay) {
    /// <summary>Attempts to detect zombie window "blocked" (hidden) by Chrome (v.8-12, maybe more)</summary>
    /// <param name="popWindow">The popup window object</param>
    /// <param name="delay">Optional delay in ms; Not necessary when called via other delayed event/function</param>
    if (window.chrome && typeof(popWindow) == 'object') {
        if (delay && !isNaN(parseInt(delay)) && delay > 0) {
            window.setTimeout(function () {
                isPopupZombie(popWindow, 0);
            }, delay);
        } else if (typeof(popWindow.opener) == 'object') {
            var w = (popWindow.innerWidth && popWindow.innerWidth > 0) ? popWindow.innerWidth : popWindow.outerWidth;
            var h = (popWindow.innerHeight && popWindow.innerHeight > 0) ? popWindow.innerHeight : popWindow.outerHeight;
            //console.log('_isPopupZombie: ' + w + ' x ' + h);
            return (w === 0 && h === 0);
        }
    }
    return false;
}

function handlePopupZombie(zombieWindow, notify, callback) {
    /// <summary>Tries to close the zombie or alert about Chrome FAIL</summary>
    /// <param name="zombieWindow">Popup window object known to be a hidden but unblocked popup</param>
    /// <param name="notify">true to notify user of hidden window, false to close zombieWindow</param>
    /// <param name="callback">optional function to call, with single argument for zombieWindow</param>
    if (window.chrome && zombieWindow) {
        if (notify === true) {
            // callback or alert
            if (typeof(callback) == 'function') {
                callback(zombieWindow);
            } else if (zombieWindow.opener) {
                zombieWindow.opener.alert("Your popup blocker has hidden a popup window instead of blocking it.\n\nPlease use the icon in the address bar to open the hidden window.");
            }
        } else {
            // try to kill the zombie
            if (zombieWindow.opener && zombieWindow.opener.console) zombieWindow.opener.console.log('Closing zombie window');
            // after close, blocker icon is in address bar, but favicon/title are not in dialog
            zombieWindow.close();
            // optional callback
            if (typeof(callback) == 'function') callback(zombieWindow);
        }
    }
}

所以我可以做类似的事情......

var popup = window.open(...);

if (typeof(popup) == 'undefined') {
    // handle it
} else if (popup && popup.closed) {
    // handle it
} else if (isPopupZombie(popup, 100)) {
    console.log('popup zombie!');
    handlePopupZombie(popup, true);
} else {
    console.log('no zombies, this is regular ordinary popup code time!');
}
4

1 回答 1

3

您可以尝试检查innerWidth、innerHeight、outerWidth 和/或outerHeight。当被阻止的铬看到让他们在 0. 喜欢:

window.addEventListener('load', function(){
    var w = window.open('');
    setTimeout(function(){
        if ((w.outerWidth === 0) && (w.outerHeight === 0)){
            alert('blocked!');
        }
        w = null;
    }, 25);
}, false);

延迟 0 将不起作用,因为对于 chrome,所有新窗口都是以 (0,0) 大小创建的,只有在一段时间后它才会给它一些适当的大小。

于 2011-06-30T20:37:31.327 回答