简体   繁体   English

Javascript 代码在 JsFiddle 上工作但在我的主机上不工作

[英]Javascript code working on JsFiddle but not working on my host

https://jsfiddle.net/n0hnu5te/1/ this script is a browser incognito mode detector and it's working well on JsFiddle but it is not working on my hosting and localhost. https://jsfiddle.net/n0hnu5te/1/这个脚本是一个浏览器隐身模式检测器,它在 JsFiddle 上运行良好,但在我的主机和本地主机上不起作用。 I tried some ways like onload etc. but nothing changed.我尝试了一些方法,如 onload 等,但没有任何改变。

Could you explain what is the problem, please?你能解释一下是什么问题吗?

My Code is:我的代码是:

 <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Title</title> </head> <body> <script> // Detect private browsing // Inpired by original gist: https://gist.github.com/cou929/7973956 // But based in general on comment: https://gist.github.com/cou929/7973956#gistcomment-1791792 and other comments (function (window) { var on, off; function Webkit() { if (window.webkitRequestFileSystem) { window.webkitRequestFileSystem(window.TEMPORARY, 1, off, on); return true; } } function Mozilla() { if ('MozAppearance' in document.documentElement.style) { const db = indexedDB.open('test'); db.onerror = on; db.onsuccess = off; return true; } } function Safari() { if (/constructor/i.test(window.HTMLElement)) { // iOS 11 // Origin: https://gist.github.com/cou929/7973956#gistcomment-2272103 try { window.openDatabase(null, null, null, null); } catch (e) { on(); } // Older Safari try { if (localStorage.length) off(); else { localStorage.x = 1; localStorage.removeItem('x'); off(); } } catch (e) { // Original gist: https://gist.github.com/jherax/a81c8c132d09cc354a0e2cb911841ff1 // Safari only enables cookie in private mode // if cookie is disabled then all client side storage is disabled // if all client side storage is disabled, then there is no point // in using private mode navigator.cookieEnabled ? on() : off(); } return true; } } function IE10Edge() { if (!window.indexedDB && (window.PointerEvent || window.MSPointerEvent)) { on(); return true; } } window.isPrivate = function (on_cb, off_cb) { on = on_cb || function () {}; off = off_cb || function () {}; Webkit() || Mozilla() || Safari() || IE10Edge() || off(); }; })(window); isPrivate( function () {document.body.innerHTML = 'Private browsing: <b>ON</b>'}, function () {document.body.innerHTML = 'Private browsing: <b>OFF</b>'} ); </script> </body> </html>

Stackoverflow wants more details but I think I wrote all details. Stackoverflow 想要更多细节,但我想我写了所有细节。 So these paragraph is not important.所以这一段并不重要。

Original jsfiddle iframe (your script run "ok"):原始 jsfiddle iframe(您的脚本运行“ok”):

<iframe name="result" allow="midi; geolocation; microphone; camera; display-capture; encrypted-media;" allowfullscreen="" allowpaymentrequest="" frameborder="0" src="" sandbox="allow-modals allow-forms allow-scripts allow-same-origin allow-popups allow-top-navigation-by-user-activation allow-downloads"></iframe>

With removed sandbox attribute (your script fail):删除sandbox属性(您的脚本失败):

<iframe name="result" allow="midi; geolocation; microphone; camera; display-capture; encrypted-media;" allowfullscreen="" allowpaymentrequest="" frameborder="0" src=""></iframe>

The problem is in the sandbox attribute that jsfiddle sets in the iframe with which sandbox your script.问题出在 jsfiddle 在 iframe 中设置的sandbox属性中,而沙箱是您的脚本。

You must avoid testing these types of scripts in an online service.您必须避免在在线服务中测试这些类型的脚本。 You can run your code as a snippet in chrome devtools or as simple html page and see that the problem is just how you detect incognito mode .您可以将代码作为 chrome devtools 中的片段或作为简单的 html 页面运行,并查看问题在于您如何检测隐身模式

Also pay attention to the order in which you check the features for the various browsers and the use or not of proprietary prefixes.还要注意检查各种浏览器功能的顺序以及是否使用专有前缀。

Keep in mind that, like all feature detections, they can change their result when the browser version changes.请记住,与所有特征检测一样,它们可以在浏览器版本更改时更改其结果。

This may help you for Chrome https://stackoverflow.com/a/27805491/3679111这可能对 Chrome 有帮助https://stackoverflow.com/a/27805491/3679111

As someone with a lot of experience in detecting private browsing modes in browsers, here's a few tips .作为在检测浏览器中的隐私浏览模式方面具有丰富经验的人,这里有一些提示

But for your code in particular:但特别是对于您的代码:

  • Your Chrome/Webkit method does not work as of Chrome 76.您的 Chrome/Webkit 方法从 Chrome 76 开始不起作用。
  • Your Firefox code is over-complicated, just check for whether navigator.serviceWorker is defined or not.您的 Firefox 代码过于复杂,只需检查 navigator.serviceWorker 是否已定义。 It's not defined in private.它不是私下定义的。
  • Apple tries to mitigate private browsing detection frequently, so your Safari code won't work for Safari 13 or higher. Apple 会频繁尝试减少隐私浏览检测,因此您的 Safari 代码不适用于 Safari 13 或更高版本。
  • MSIE code is functional and will work. MSIE 代码可以正常工作并且可以工作。

Please note this was written in November 2021. Methods for detecting private browsing modes is an arms race, so this advice may no longer hold up in the future.请注意,本文写于 2021 年 11 月。检测隐私浏览模式的方法是一场军备竞赛,因此此建议将来可能不再适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM