在 Code.org 的 App Lab 编辑器中,我们最近开始在 Chrome 64 中看到这个错误:
Uncaught DOMException: Failed to read the 'rules' property from 'CSSStyleSheet'
在这个函数中抛出错误,该函数旨在检测浏览器是否正在使用 CSS 媒体查询,在包含styleSheets[i].cssRules
.
/**
* IE9 throws an exception when trying to access the media field of a stylesheet
*/
export function browserSupportsCssMedia() {
var styleSheets = document.styleSheets;
for (var i = 0; i < styleSheets.length; i++) {
var rules = styleSheets[i].cssRules || styleSheets[i].rules;
try {
if (rules.length > 0) {
// see if we can access media
rules[0].media;
}
} catch (e) {
return false;
}
}
return true;
}
该问题已在 Windows、OSX、Ubuntu 和 ChromeOS 上发现;在 Chrome 版本 64.0.3282.167 和 64.0.3282.186 上。然而,我们也发现这个问题并没有发生在完全相同的 Chrome 版本和平台上——而且我们似乎无法在隐身窗口中重现该问题。
这个错误的根本原因是什么?