有什么方法可以快速确定浏览器是否支持启用 CORS 的图像而不污染浏览器?

发布于 2024-12-20 05:35:02 字数 194 浏览 3 评论 0原文

是否有快速测试来确定浏览器是否支持支持 CORS 的图像,并且在其上绘制时不会污染画布。我知道 Chrome 15 支持此功能,Firefox 9Beta 但不支持 Firefox 8,Safari 不支持,IE9 不支持。但必须有一个非常简单的测试来确定这一点,基本上是在画布上绘制图像,并查看当您尝试获取图像数据时是否出现异常,或者是否有任何其他简单的方法来确定这一点。

Is there a quick test to determine if a browser supports CORS-enabled images not tainting a canvas when drawn on them. I know Chrome 15 supports this, Firefox 9Beta but not Firefox 8, Safari doesn't, IE9 doesn't. But there must be a pretty simple test to determine this, is basically drawing on a canvas with an image and seeing if you get an exception when you try to get image data, or is there any other easy way to determine this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燕归巢 2024-12-27 05:35:02

这似乎有效:

if ('crossOrigin' in new Image())
    // ...

This seems to work:

if ('crossOrigin' in new Image())
    // ...
谷夏 2024-12-27 05:35:02

以下是我测试 CORS 污染画布支持的方法。如果有人有一种无需加载图像的方法,请在这里发布:

function CanvasFunctions() {
    var _initialized = false, _corsNotSupported = false;


    function DrawImage(image, src) {
        jQuery.when(initialized()).then(function () { 
            if (_corsNotSupported) {
               image.src = GetProxyImage(src);
            } else {
               image.src = src;
            } 
        }
    }

    function initialized() {
        if (_initialized) {
            return true;
        }

        var dfd = $.Deferred();

        var src = 'http://example.com/corsImage.jpg',
            image.onload = function () {
                var canvas = document.createElement('canvas');
                canvas.width = 250;
                canvas.height = 250;
                var ctx = canvas.getContext('2d');
                ctx.drawImage(image, 0, 0, 200, 200);
                try {
                    var hit = ctx.getImageData(0, 0, 1, 1).data[3] > 1;
                    console.log('Canvas was not tainted by CORS image, hit: ' + hit);
                } catch (e)  {
                    console.log('Canvas was tainted by CORS image, reverting to passthru for images');
                    _corsNotSupported = true;
                }
                _initialized = true;
                dfd.resolve(true);
            });

        image.src = src;


        return dfd.promise();
    }
}

Here is how I tested for CORS tained canvas support. If someone has a way without having to load an image, please post it here:

function CanvasFunctions() {
    var _initialized = false, _corsNotSupported = false;


    function DrawImage(image, src) {
        jQuery.when(initialized()).then(function () { 
            if (_corsNotSupported) {
               image.src = GetProxyImage(src);
            } else {
               image.src = src;
            } 
        }
    }

    function initialized() {
        if (_initialized) {
            return true;
        }

        var dfd = $.Deferred();

        var src = 'http://example.com/corsImage.jpg',
            image.onload = function () {
                var canvas = document.createElement('canvas');
                canvas.width = 250;
                canvas.height = 250;
                var ctx = canvas.getContext('2d');
                ctx.drawImage(image, 0, 0, 200, 200);
                try {
                    var hit = ctx.getImageData(0, 0, 1, 1).data[3] > 1;
                    console.log('Canvas was not tainted by CORS image, hit: ' + hit);
                } catch (e)  {
                    console.log('Canvas was tainted by CORS image, reverting to passthru for images');
                    _corsNotSupported = true;
                }
                _initialized = true;
                dfd.resolve(true);
            });

        image.src = src;


        return dfd.promise();
    }
}
翻身的咸鱼 2024-12-27 05:35:02

确定浏览器是否支持 CORS 的最简单方法是查找 XHR 的 withCredentials 属性。 IE 使用 XDomainRequest 对象而不是 XHR,因此您还需要查找该对象:

function supportsCors() {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    // Supports CORS
    return true;
  } else if (typeof XDomainRequest != "undefined") {
    // IE
    return true;
  }
  return false;
}

The simplest way to determine if a browser supports CORS is to look for the XHR's withCredentials property. IE uses the XDomainRequest object instead of XHR, so you need to look for that as well:

function supportsCors() {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    // Supports CORS
    return true;
  } else if (typeof XDomainRequest != "undefined") {
    // IE
    return true;
  }
  return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文