【发布时间】:2011-11-07 21:01:05
【问题描述】:
我刚刚发现 Google 的 Screen Capture 扩展程序使我的网站的 window.onresize 事件不会触发。
我想执行 javascript 检查以查看用户是否安装了 ScreenCapture,如果是,则警告用户该问题。
一年前我想我听说过一些 javascript 代码可以做到这一点,可能使用了一些谷歌 API,但我不记得了。
对此有何见解?我还没有开发任何扩展,所以我真的不知道它们是如何工作的。
[编辑] 所以我被要求展示一些代码。正如我之前的问题 (window.onresize not firing in Chrome but firing in Chrome Incognito) 中所见,问题发生在任何 window.onresize 事件函数上,所以我认为我的代码并不重要。
另外,我的代码很多,我不知道要粘贴多少或是否有帮助。
var debounce = function (func, threshold, execAsap)
{
var timeout;
return function debounced () {//alert("1.1 Y U NO WORK?");
var obj = this, args = arguments;
function delayed () {
if (!execAsap)
func.apply(obj, args);
timeout = null;
}
if (timeout)
clearTimeout(timeout);
else if (execAsap)
func.apply(obj, args);
timeout = setTimeout(delayed, threshold || 100);
};
};
window.onresize = debounce(function (e) { //alert("1.2 Y U NO WORK?");
flag = true;
var point = window.center({width:1,height:1});
doCenter(point);
// does something here, but only once after mouse cursor stops
}, 100, false);
我想强调的是,问题不是由去抖引起的。 window.onresize = t; function t (e) { alert("wtf?");} 也不行。
[编辑2]
结果如下:
var screenCapture = null;
var screenCaptureImg = document.createElement("img");
screenCaptureImg.setAttribute("src", "chrome-extension://cpngackimfmofbokmjmljamhdncknpmg/images/arrow.png");
/*
* Add event listeners for both "load"- and "error"-event
* Set the variable showing the existence of the extension by
* setting it to "true" or "false" according to the fired event
*/
screenCaptureImg.addEventListener("load", doLoad, false);
function doLoad(e){
screenCapture = true; //removeImgTag(e);
alert("I've so cleverly detected that your Chrome has the ScreenCapture extension enabled. \n\nThis extension interferes with my website's DOM and long story short, it won't be able to scale properly.\n\nSo please disable it. \nConsider this extension: \"Disable All Extensions Plus\", it's a handy selective disabler.");
}
screenCaptureImg.addEventListener("error", function(e){
screenCapture = false; //removeImgTag(e);
}, false);
/*
function removeImgTag(e) {
e.currentTarget.parentNode.removeChild(e.currentTarget);
}
*/
请注意,我无法让 removeImgTag 工作,因为(至少在 chrome 中),我似乎无权访问 document 对象以从我的页面创建或删除元素,从在这些事件函数中。这也是为什么我显示alert 而不是优雅地写document.getElementById("something").innerHTML=...
【问题讨论】:
-
@ScottA - 不幸的是,检测似乎是针对您自己的扩展,而不是网站 js 端检测。 twodordan - 它绝对不会让你的 onresize 事件不触发,尽管它可能会取消效果。粘贴您的代码,有人可能会想出一个解决方法。
-
这个扩展也是你的吗? (可以修改吗?)
-
@pimvdb 不,它是由 Google 开发的。
-
@mrtsherman 这是我找到罪魁祸首的问题:stackoverflow.com/questions/8041730/…
标签: javascript google-chrome google-chrome-extension