如何使用JavaScript检测Safari浏览器中是否启用了“防止跨站点跟踪”功能

12

有人能告诉我如何使用JavaScript检测Safari浏览器是否启用了防止第三方跟踪保护吗?


你最终解决了这个问题吗? - Bistro
1个回答

0

您可以通过使用 iframe 进行快速测试,查看是否能够设置/读取第三方域名的 cookie。

例如:

<!DOCTYPE html>
<html>
<head>
  <script>
    function testThirdPartyCookies(callback) {
      let iframe = document.createElement('iframe');
      iframe.style.display = 'none';
      iframe.src = 'https://thirdparty.domain.com'; // Replace with a third-party domain

  iframe.onload = function () {
    try {
      // Attempt to set a test cookie on the third-party domain
      iframe.contentWindow.document.cookie = 'testcookie=1; path=/';

      // Check if the test cookie was set successfully
      let cookieEnabled = iframe.contentWindow.document.cookie.includes('testcookie=1');
      callback(cookieEnabled);
    } catch (error) {
      // An error occurred, which might indicate that third-party cookies are blocked
      callback(false);
    } finally {
      // Remove the iframe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接