2017-01-06 1 views
0

ユーザーが通常モードでページを訪れた場合、私は訪問者がシークレットモードを使用しているか否かを検出クロスブラウザのJavaScriptを作成しようとしている、と警告メッセージを与えています。Javascriptのためのクロスブラウザシークレットモード(プライベートブラウジング)の検出

現在、私はChromeとOperaの上だけで正常に動作するスクリプトを、持っている、しかし、私はそれは同様のfirefox、サファリのような他のすべてのブラウザ上で動作を取得する必要があり、エッジなど

私のスクリプトは、(上の作業しますChromeとOperaは)次のとおりです。

<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){ 
function main() { 
    var fs = window.RequestFileSystem || window.webkitRequestFileSystem; 
    if (!fs) { 
    alert("check failed!"); 
    return; 
    } 
    fs(window.TEMPORARY, 100, function(fs) { 
    alert("You are not using Incognito Mode!"); 
    }); 
} 
main(); 

}//]]> </script> 

私はすべての主要なWebブラウザで同じアラートの結果を与えるために、このように単一のスクリプトを書くのを手伝ってください。私は最終的にだけでなくFirefox用の作業スクリプトを作った

おかげ

UPDATEを。コードは以下の通りである:

<script type='text/javascript'> 
var db; 
var request = indexedDB.open("MyTestDatabase"); 
request.onsuccess = function(event) { 
if (navigator.userAgent.indexOf("Firefox") != -1) 
{ 
    alert("You are not using Incognito Mode!"); 
}; 
};</script> 

それはFirefoxのみではなく、クロームに実行されるように、私は「もし(navigator.userAgent.indexOf(」Firefoxの「)= -1!)」機能を使用しましたまたは他のブラウザを使用します。

UPDATE:

[OK]を別の成果!私はSafariのスクリプトも正常に作成しました。ここでは、次のとおりです。繰り返しますが、私が使用した

<script type='text/javascript'> 
try { localStorage.test = 2; } catch (e) { 
} 
if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) 
{ 
if (localStorage.test = "true") { 
alert("You are not using Incognito Mode!"); 
} 
} 
</script> 

"もし(navigator.userAgent.search(" サファリ ")> = 0 & & navigator.userAgent.search(" クローム ")< 0)" 機能Safariでのみ実行され、他のブラウザでは実行されません。

私はEdgeブラウザ専用のスクリプトを書くのに助けが必要です。

+0

そして、これらのどれ? https://www.google.com/search?q=javascript+detect+incognito+mode+firefox - この – mplungjan

+0

の調査であなたの努力を分かち合い、firefoxでindexedDBを使用することはできません。そして、これを実現するスクリプトは一つもありません。 –

+0

はクローム58それはお返事に感謝@codyクローム63 –

答えて

0

私はhereを発見したこのソリューションがあります:

それが削除される場合には、私はちょうど下にそれをコピーしたが。助け

function retry(isDone, next) { 
    var current_trial = 0, max_retry = 50, interval = 10, is_timeout = false; 
    var id = window.setInterval(
     function() { 
      if (isDone()) { 
       window.clearInterval(id); 
       next(is_timeout); 
      } 
      if (current_trial++ > max_retry) { 
       window.clearInterval(id); 
       is_timeout = true; 
       next(is_timeout); 
      } 
     }, 
     10 
    ); 
} 

function isIE10OrLater(user_agent) { 
    var ua = user_agent.toLowerCase(); 
    if (ua.indexOf('msie') === 0 && ua.indexOf('trident') === 0) { 
     return false; 
    } 
    var match = /(?:msie|rv:)\s?([\d\.]+)/.exec(ua); 
    if (match && parseInt(match[1], 10) >= 10) { 
     return true; 
    } 
    return false; 
} 

function detectPrivateMode(callback) { 
    var is_private; 

    if (window.webkitRequestFileSystem) { 
     window.webkitRequestFileSystem(
      window.TEMPORARY, 1, 
      function() { 
       is_private = false; 
      }, 
      function(e) { 
       console.log(e); 
       is_private = true; 
      } 
     ); 
    } else if (window.indexedDB && /Firefox/.test(window.navigator.userAgent)) { 
     var db; 
     try { 
      db = window.indexedDB.open('test'); 
     } catch(e) { 
      is_private = true; 
     } 

     if (typeof is_private === 'undefined') { 
      retry(
       function isDone() { 
        return db.readyState === 'done' ? true : false; 
       }, 
       function next(is_timeout) { 
        if (!is_timeout) { 
         is_private = db.result ? false : true; 
        } 
       } 
      ); 
     } 
    } else if (isIE10OrLater(window.navigator.userAgent)) { 
     is_private = false; 
     try { 
      if (!window.indexedDB) { 
       is_private = true; 
      }     
     } catch (e) { 
      is_private = true; 
     } 
    } else if (window.localStorage && /Safari/.test(window.navigator.userAgent)) { 
     try { 
      window.localStorage.setItem('test', 1); 
     } catch(e) { 
      is_private = true; 
     } 

     if (typeof is_private === 'undefined') { 
      is_private = false; 
      window.localStorage.removeItem('test'); 
     } 
    } 

    retry(
     function isDone() { 
      return typeof is_private !== 'undefined' ? true : false; 
     }, 
     function next(is_timeout) { 
      callback(is_private); 
     } 
    ); 
} 
関連する問題