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

Bas*_*j J 10 javascript safari cookie-policy

有人可以告诉我如何使用 JavaScript 检测是否在 Safari 浏览器中启用了防止第三方保护。

小智 0

您可以通过使用 iframe 进行快速测试来检查是否能够使用第 3 方域设置/读取 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
Run Code Online (Sandbox Code Playgroud)