检查用户是否安装了某个扩展
我刚刚发现 Screen Capture by Google 扩展使我网站的 window.onresize 事件无法触发。
我想执行 JavaScript 检查以查看用户是否安装了 ScreenCapture,如果是,则警告用户该问题。
一年前,我想我听说过一些 javascript 代码可以做到这一点,也许使用一些谷歌 API,但我不记得了。
对此有何见解?我还没有开发任何扩展,所以我真的不知道它们是如何工作的。
[编辑] 所以我被要求展示一些代码。正如我之前的问题( window.onresize不是在 Chrome 中触发,而是在 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?");}
也不起作用。
[EDIT2]
这是结果:
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=
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要检测 Chrome 中是否安装了扩展程序,您可以检查扩展程序中是否包含已知资源(例如图像)。使用以下 URL 模式引用扩展的资源:
基本检测技术涉及创建隐藏图像标记并将加载和错误事件附加到其上,以查看图像是否加载(如 此处(对于 Firefox):
检查 Chrome 配置中扩展程序的安装目录,以查找可能的检测目标。在我的 Linux 工作站上,扩展程序位于:
您可以看到我现在安装了 3 个扩展程序:
看起来奇怪的名称是扩展程序上传到 Chrome 网上应用店时为其提供的唯一 ID。您可以从网上商店获取 ID,也可以转到“扩展”选项卡(扳手 ->“扩展”)并将鼠标悬停在相关扩展的链接上,或者在本例中为“屏幕捕获(由 Google)”(请注意带星号的部分)来获取 ID。扩展ID):
扩展目录中会有一个或多个版本;你可以忽略这一点。版本目录中是扩展程序的实际内容:
对于屏幕捕获扩展程序,有许多要使用的图像:
这些可以在此 URL 下引用:
这种技术显然取决于扩展程序内容的稳定性。我建议使用看起来可能在所有版本中保留的图像。
如上所述,可以使用相同的技术来检测 Firefox 扩展。在这种情况下,内容 URL 如下所示:
在我的 Linux 工作站上,Firefox 扩展位于:
其中看起来像这样:“h4aqaewq.default”
您可以看到我现在安装了 2 个扩展,其中一个是目录安装,另一个是 XPI(发音为“zippy”) 文件:
“staged”目录是 Firefox 保存将要更新的扩展的地方(我认为)。带括号的GUID目录是基于目录的扩展安装,.xpi文件是Firebug。
注意:XPI 即将消失(请参阅上面的链接)。它基本上是一个 zip 文件,任何理解 zip 的人都可以打开和检查。我用的是Emacs。
在 Firefox 中查找扩展 ID 有点复杂。转到“工具 -> 附加组件”,单击“扩展”选项卡,单击扩展描述旁边的“更多”链接,然后单击“评论”链接转到 Firefox 扩展站点并从 URL 中获取 ID (注意带星号的扩展 ID):
可能有一种更简单的方法可以做到这一点;欢迎提出建议。
TODO:如何在 Firefox 扩展中查找可能的图像。
额外说明,在 Chrome 中,您只能通过页面的共享 DOM 与扩展程序进行通信:主机页通信
To detect if an extension is installed in Chrome, you can check for a known resource included in the extension such as an image. Resources for the extension are referenced using the following URL pattern:
The basic detection technique involves creating a hidden image tag and attaching load and error events to it to see if the image loads (as described here for Firefox):
Check the installation directory of the extension in the Chrome configuration to find a likely target for detection. On my Linux workstation extensions are located in:
You can see that I have 3 extensions installed right now:
The odd looking names are the unique IDs given to the extension when it is uploaded to the Chrome webstore. You can obtain the ID either from the webstore or by going to the Extensions tab (wrench -> Extensions) and hovering over the link to the extension in question, or "Screen Capture (by Google)" in this case (note the asterisked extension ID):
In the extension directory there will be one or more versions; you can ignore this. Within the version directory is the actual content of the extension:
In the case of the Screen Capture extension there are a number of images to use:
These can be referenced under this URL:
This technique obviously depends on the stability of the content of the extension. I recommend using an image that looks likely to remain through all versions.
As mentioned above, the same technique can be used to detect Firefox extensions. In this case the content URL looks like this:
On my Linux workstation Firefox extensions are located in:
Where <USER PROFILE ID> looks something like this: "h4aqaewq.default"
You can see that I have 2 extensions installed right now, one of which is a directory installation and the other of which is a XPI (pronounced "zippy") file:
The "staged" directory is where Firefox keeps extensions that will be updated (I think). The GUID directory with the brackets is a directory-based extension installation, and the .xpi file is Firebug.
Note: XPI is going away (see the link above). It's basically a zip file that can be opened and inspected by anything that understands zip. I used Emacs.
Finding the extension ID in Firefox is a bit more involved. Go to "Tools -> Add-ons", click the Extensions tab, click the "More" link next to the extension description, then click the "reviews" link to go to the Firefox extension site and get the ID from the URL (note the asterisked extension ID):
There's probably an easier way to do this; suggestions welcome.
TODO: how to find a likely image in a Firefox extension.
As an extra note, in Chrome you can only communicate with an extension via the shared DOM of the page: Host page communication