【问题标题】:Trying to detect google chrome cast and stop it slowing things down试图检测 google chromecast 并阻止它减慢速度
【发布时间】:2016-11-05 15:21:50
【问题描述】:
window.onerror=function(err,url,line){
    if(err=='boom'){
        console.log(true);
        }
    else{
        console.log(false);
        }};

console.error('boom');

这会输出undefined!

如何在 java 脚本中判断错误包含什么文本?

我正在尝试检测 google chrome cast 扩展错误

加载资源失败:net::ERR_ADDRESS_UNREACHABLE chrome-extension://boadgeojelhgndaghljhdcfkmllpafd/cast_sender.js

相关:Google chrome cast sender error if chrome cast extension is not installed or using incognito

我的理由是

//​tried to detect chromecast extension id: boadgeojelhgndaghljhdicfkmllpafd
//it looks like it is protected from detection (tried: http://blog.kotowicz.net/2012/02/intro-to-chrome-addons-hacking.html)
//if they are using:
//chrome flash           ===flash     (needs double click as &autoplay=1 lets the user do only one click but it side-effects with no video preview)
//chrome       chromeCast===fastjs    (google are being bad players here!)
//chrome                 ===slowjs   (almost unusable website)
//chrome flash chromeCast===flash    ffsake! (maybe try one video then see if there are errors and put a cookie, chromecast=true means that we got no errors so we don't need flash!) todo:how to analyse error text?
//if the user is on chrome and does not have flash then they are youtubes 'exception'! they will have very crap loading times


var chrome=0;
if(/Chrome/.test(navigator.userAgent)&&/Google Inc/.test(navigator.vendor)){
    chrome=1;
    function detectPlugin(substrs){
        if(navigator.plugins){
            for(var i=0,l=navigator.plugins.length;i<l;i++){
                var plugin=navigator.plugins[i]
                ,   haystack=plugin.name+plugin.description
                ,   found=0;
                for(var j=0;j<substrs.length;j++){
                    if(haystack.indexOf(substrs[j])!=-1){found++;}
                    }
                if(found==substrs.length){return true;}
                }}
            return false;
            }
        var detectFlash=[/*"Shockwave",*/"Flash"]; //not entirely sure how relevant shockwave is here
        if(detectPlugin(detectFlash)){chrome=2;}
        }

然后我创建一个 iframe:

'https://www.youtube.com/'+(chrome==2?'v':'embed')+'/'+ytvidcode

ytvidcode 是 youtube 视频的 ID

v 用于已折旧的闪存嵌入,而embed 用于 html5(损坏/wont fix)嵌入

所以上面的代码会检测到 chrome 然后会尝试检测 flash 但无法检测到 chrome-cast。

如果用户同时拥有 chrome-cast 和 flash(因为它无法检测到 chrome-cast/chrome-cast 错误),它将强制使用 flash(如果存在)。

如果 Flash 不存在,那么用户将获得糟糕的 youtube 嵌入体验(他们可能不得不使用其他浏览器)

是否有可能检测到 chrome-cast 造成的错误或任何其他存在的痕迹?

因为如果它是一个已安装的扩展,那么它应该被充分利用,因为闪存已贬值


window.onerror=function(e,url,line){console.dir(arguments);}//nope

window.onerror=function(e,url,line){console.dir(this);}//nope

https://github.com/Valve/fingerprintjs2

new Fingerprint2().get(function(result, components){
    console.log(result); //a hash, representing your device fingerprint
    console.log(components); // an array of FP components
    });

components[13] ("regular_plugins") 显示没有 Google cast,即使我今天已经安装了它


此时我唯一能想到的就是计时加载他们的一个视频(如果他们在 chome 上并且加载测试视频的时间太长,那么他们一定没有 chrome cast),使用这个https://developers.google.com/youtube/iframe_api_reference#Getting_Started 和一个计时器和一个 cookie 之后(所以测试只发生一次)... 我会测试这个然后也许会报告回来

【问题讨论】:

    标签: javascript google-chrome-extension youtube


    【解决方案1】:

    这让视频使用embed,如果不是 chrome,或者如果它们在启用 cast 扩展的 chrome 上。

    如果他们在没有强制转换的情况下使用 chrome,那么它会检查 flash,如果他们支持 flash,那么它会使用 v

    如果他们有没有ether cast或flash的chrome(他们只会得到chrome cast错误,那是他们的错!)

    v 用于已折旧的闪存嵌入,而embed 用于 html5(损坏/无法修复)嵌入。

    也许可以改进...

    <!DOCTYPE html>
    <html>
        <head>
            <script type="text/javascript" src="//www.gstatic.com/cv/js/sender/v1/cast_sender.js"></script>
            <script type="text/javascript">
                function proceed(how){
                    document.getElementById('vid').innerHTML='<iframe src="https://www.youtube.com/'+how+'/M_lHI1opADk"></iframe>';
                    console.log(ischrome,iscast);
                    }
                var ytv=['embed','v','embed','v'];
                var ischrome=0;
                var iscast=true;//maybe
                if(/Chrome/.test(navigator.userAgent)&&/Google Inc/.test(navigator.vendor)){
                    ischrome=1;
                    setTimeout(function(){
                        try{new chrome.cast.SessionRequest('794B7BBF');}
                        catch(e){console.log(e);
                            iscast=false;}
                        finally{
                            (iscast==true)&&(ischrome=2);
                            if(ischrome==1){
                                function hasflash(){if(navigator.plugins){for(var i=0,l=navigator.plugins.length;i<l;i++){if((navigator.plugins[i].name+navigator.plugins[i].description).indexOf("Flash")!=-1){return true;}}}return false;}
                                if(hasflash()){ischrome=3;}
                                proceed(ytv[ischrome]);
                                }
                            else{
                                proceed(ytv[ischrome]);
                                }}},1000);}
                else{proceed(ytv[ischrome]);}
            </script>
        </head>
        <body>
            <div id="vid"></div>
        </body>
    </html>
    

    您可以通过安装强制转换扩展禁用/启用和刷新页面来测试这一点。

    【讨论】:

    猜你喜欢
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多