检查用户是否安装了某个分机

Spe*_*ump 9 javascript google-chrome google-chrome-extension

我刚刚发现Screen Extension 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);
Run Code Online (Sandbox Code Playgroud)

我想强调的是,问题不在于去抖.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);
    }
    */
Run Code Online (Sandbox Code Playgroud)

请注意,我无法开始removeImgTag工作,因为(至少在chrome中),我似乎无法访问该document对象,以便从这些事件函数中创建或删除页面中的元素.这也是为什么我要展示一个alert而不是优雅地写一个document.getElementById("something").innerHTML=...

Sco*_*t A 16

要检测Chrome中是否安装了扩展程序,您可以检查扩展程序中包含的已知资源,例如图像.使用以下URL模式引用扩展的资源:

chrome-extension://<extensionID>/<pathToFile>
Run Code Online (Sandbox Code Playgroud)

基本的检测技术包括创建隐藏的图像标记并将加载和错误事件附加到它以查看图像是否加载(如此针对Firefox所述):

extensionImg.setAttribute("src", "chrome-extension://<INSERT EXTENSION ID HERE>/images/someImage.png"); // See below for discussion of how to find this

/*
 * 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
 */
extensionImg.addEventListener("load", function(e) {
    extensionExists = true;
    removeImgTag(e);
}, false);
extensionImg.addEventListener("error", function(e) {
    extensionExists = false;
    removeImgTag(e);
}, false);

function removeImgTag(e) {
    e.currentTarget.parentNode.removeChild(e.currentTarget);
}
Run Code Online (Sandbox Code Playgroud)

检查Chrome配置中扩展程序的安装目录,以查找可能的检测目标.在我的Linux工作站上,扩展程序位于:

 ~/.config/chromium/Default/Extensions
Run Code Online (Sandbox Code Playgroud)

您可以看到我现在安装了3个扩展程序:

~/.config/chromium/Default/Extensions$ ls
cpecbmjeidppdiampimghndkikcmoadk  nmpeeekfhbmikbdhlpjbfmnpgcbeggic
cpngackimfmofbokmjmljamhdncknpmg
Run Code Online (Sandbox Code Playgroud)

看起来很奇怪的名称是在将扩展程序上传到Chrome网上应用店时为其添加的唯一ID.您可以从网上商店获取ID,也可以转到扩展程序选项卡(扳手 - >扩展程序)并将鼠标悬停在相关扩展程序的链接上,或者在此情况下使用"屏幕截图(Google)"(请注意带星号的扩展程序) ID):

https://chrome.google.com/webstore/detail/**cpngackimfmofbokmjmljamhdncknpmg**
Run Code Online (Sandbox Code Playgroud)

在扩展目录中,将有一个或多个版本; 你可以忽略这个.在版本目录中是扩展的实际内容:

~/.config/chromium/Default/Extensions/cpngackimfmofbokmjmljamhdncknpmg/5.0.3_0$ ls
account.js         images             page.js         sina_microblog.js
ajax.js            isLoad.js          picasa.js       site.js
background.html    _locales           plugin          style.css
editor.js          manifest.json      popup.html      ui.js
facebook.js        notification.html  sha1.js         upload_ui.js
hotkey_storage.js  oauth.js           shortcut.js
hub.html           options.html       showimage.css
i18n_styles        page_context.js    showimage.html
Run Code Online (Sandbox Code Playgroud)

对于Screen Capture扩展,有许多图像可供使用:

~/.config/chromium/Default/Extensions/cpngackimfmofbokmjmljamhdncknpmg/5.0.3_0/images$ ls
arrow.png                icon_128.png    icon_save.png     print.png
copy.png                 icon_16.png     line.png          region.png
cross.png                icon_19.png     loading.gif       screen.png
custom.png               icon_32.png     loading_icon.gif  sina_icon.png
delete_account_icon.png  icon_48.png     mark.png          toolbar_bg.png
down_arrow.png           icon_close.png  picasa_icon.png   upload.png
facebook_icon.png        icon_copy.png   popup_bg.jpg      whole.png
Run Code Online (Sandbox Code Playgroud)

这些可以在此URL下引用:

chrome-extension://cpngackimfmofbokmjmljamhdncknpmg/images/arrow.png
Run Code Online (Sandbox Code Playgroud)

这种技术显然取决于扩展内容的稳定性.我建议使用看起来可能保留在所有版本中的图像.


如上所述,可以使用相同的技术来检测Firefox扩展.在这种情况下,内容URL如下所示:

chrome://<EXTENSION NAME>/content/<PATH TO RESOURCE>
Run Code Online (Sandbox Code Playgroud)

在我的Linux工作站上,Firefox扩展位于:

 ~/.mozilla/firefox/<USER PROFILE ID>/extensions
Run Code Online (Sandbox Code Playgroud)

其中<USER PROFILE ID>看起来像这样:"h4aqaewq.default"

您可以看到我现在安装了2个扩展,其中一个是目录安装,另一个是XPI(发音为"zippy")文件:

~/.mozilla/firefox/h4aqaewq.default/extensions$ ls
{3e9a3920-1b27-11da-8cd6-0800200c9a66}  staged
firebug@software.joehewitt.com.xpi
Run Code Online (Sandbox Code Playgroud)

"暂存"目录是Firefox保留将要更新的扩展(我认为).带括号的GUID目录是基于目录的扩展安装,.xpi文件是Firebug.

注意:XPI正在消失(参见上面的链接).它基本上是一个zip文件,任何能理解zip的东西都可以打开和检查.我用过Emacs.

在Firefox中查找扩展ID更为复杂.转到"工具 - >附加组件",单击"扩展"选项卡,单击扩展说明旁边的"更多"链接,然后单击"评论"链接转到Firefox扩展站点并从URL获取ID(注意带星号的扩展ID):

https://addons.mozilla.org/en-US/firefox/addon/**firebug**/reviews/?src=api
Run Code Online (Sandbox Code Playgroud)

这可能是一种更简单的方法; 建议欢迎.

TODO:如何在Firefox扩展中查找可能的图像.


另外请注意,在Chrome中,您只能通过页面的共享DOM与扩展程序进行通信:主机页面通信