- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在 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 版本和平台上——而且我们似乎无法在隐身窗口中重现该问题。
此错误的根本原因是什么?
最佳答案
这是一个很好的故事,也是网络开发人员的一个新“难题”,所以我不得不分享:
Chrome 64.0.3282.0(2018 年 1 月发布,full change list)对样式表的安全规则进行了更改。我很恼火,因为我在任何比完整提交列表更详细的变更日志中找不到此更改。
提交 a4ebe08在 Chromium 中描述:
Update behavior of CSSStyleSheet to match spec for Security origin
Spec is here: https://www.w3.org/TR/cssom-1/#the-cssstylesheet-interface
Updated: the following methods now throw a SecurityError if the style sheet is not accessible:
- cssRules() / rules()
- insertRule()
- deleteRule()
此提交修复了错误 Security: Inconsistent CORS implementation regarding CSS and the link element.链接的 W3C 规范详细描述了在何处使用 CSS 对象模型需要同源访问。
综上所述,为什么这个问题会出现在 App Lab 中?我们不应该遇到任何 CORS 问题,因为我们只从我们自己的来源加载样式表:
最后的线索是我们无法在私有(private)选项卡中重现此问题。我们开始查看 Chrome 扩展并意识到一些受影响的用户有 Loom Video Recorder启用扩展,似乎将自己的 CSS 注入(inject)页面。由于我们的(原始)函数正在遍历所有 加载的样式表,它试图访问由扩展注入(inject)的样式表,从而导致 CORS 错误。
也就是说,围绕 Chrome 中的这一变化仍然存在一些悬而未决的问题和争论:
try/catch
。To fix our problem, we just tore out the entire function.我们不再支持 IE9,而且我们知道我们支持的所有浏览器都能正确处理媒体查询。
相关(但不完全重复)的问题:
关于javascript - 未捕获的 DOMException : Failed to read the 'rules' property from 'CSSStyleSheet' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49161159/
我知道document.styleSheets它由页面中的所有有效样式表组成。我想知道我是否可以创建一个新的并通过 javascript 将其附加到当前列表。 我试过document.styleShe
我不知道我哪里错了:/。当我运行这段代码时,我得到的只是一个空白元素。我似乎无法让 insertRule 方法执行任何操作(甚至不产生错误)。我错过了什么吗? Test v
我无法弄清楚为什么 .cssRules和 .rules在我简单的颜色生成器元素中不起作用。 我有一个 元素链接我的外部 CSS 文件: 还有一个 在 之前链接我的外部 JS 文件的元素元素: 然
我一直在玩 document.styleSheets API,它主要做我想做的,但是一旦我添加了一堆规则,我希望能够将 shylesheet 对象解析为有效的 css(这样我就可以将它放入一个可下载的
我想通过JavaScript更好的控制CSS变量,需要对CSSRule对象进行操作。 这是获取它的 2 种方法的 JavaScript 代码: // get from the CSSStyleShee
CSSStyleSheet 将区域设置应用于其值是 macOS High Sierra 上 Chrome 版本 65.0.3325.146 中的错误吗?以下 html 重现了错误。它针对俄语系统区域设
我已经阅读了有关使用 Javascript 插入 css 规则的内容,并设法使其正常工作(经过一些试验和错误)。所以我有两个问题: Q.1 为什么索引 的克隆部分文档 有 3 styleSheets
是否可以从 HTMLStyleElement ( document.styleSheets[0] ) 中获取 CSSStyleSheet 对象 ( document.createElement('st
我在使用此代码的 Internet Explorer 11 中遇到“IndexSizeError”错误: var style = document.createElement('style'); do
我尝试使用基于 https://www.w3.org/wiki/Dynamic_style_-_manipulating_CSS_with_JavaScript 的 typescript 插入一个 c
一个对应一个CSSStyleSheet对象(在document.styleSheets中),但是怎么找,比如CSSStyleSheet对象对应... console.log(document.styl
在 Code.org 的 App Lab 编辑器中,我们最近开始在 Chrome 64 中看到这个错误: Uncaught DOMException: Failed to read the 'rule
我正在将 Selenium 与 Java 和 Chrome/Firefox 驱动程序一起使用。 FirefoxProfile profile = new FirefoxProfile(); profi
我的代码中出现一个错误,该错误似乎只发生在 Android 版 Chrome 67+ 上。我在使用 Chrome 的任何其他平台上都没有遇到这个问题。这种情况偶尔会发生。 错误信息:SecurityE
啊!我的网站在 Chrome 中损坏了。 在控制台中获取此消息:Uncaught DOMException: Failed to execute 'insertRule' on 'CSSStyleSh
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关于您编写的代码问题的问题必须在问题本身中描述具体问题 — 并且包括有效代码 以重现它。参见 SSC
我是一名优秀的程序员,十分优秀!