无法访问样式表 CORS 的 cssRules
所以我一直在读到您无法访问外部样式表的 cssRules,因为它会遇到 CORS 策略问题。
我决定采取不同的方法,但仍然遇到问题。
我的方法:
- 在后端下载 css 文件并将其上传到 S3 存储桶
- 返回现有链接和新链接以进行匹配
- 删除现有
link
标签并添加一个指向我的 CDN 的新标签 - 访问
document.styleSheets
- Tadaaaa (但这失败了)
我想弄清楚的是,如果我的 CDN 允许从任何来源进行访问,为什么我仍然遇到问题?
export default () => {
const payload = [...document.styleSheets].filter(s => s.href).map(s => s.href);
axios.post('SOME ENDPOINT', { css: payload }).then(({ status, data: { data: newLinks } }) => {
if (status === 200) {
for (const i in newLinks) {
document.querySelector(`link[href="${newLinks[i].source}"]`).remove()
const stylesheet = document.createElement('link');
stylesheet.rel = 'stylesheet';
stylesheet.href = newLinks[i].downloaded;
document.getElementsByTagName('head')[0].appendChild(stylesheet);
}
}
}).then(() => {
let delay = 250
setTimeout(() => {
console.log('Stylesheets with Removed Links', [...document.styleSheets]);
}, delay)
}).then(() => {
console.log([...document.styleSheets])
})
}
Safari 上出现错误 SecurityError: 不允许访问跨域样式表
我有看到这个链接无法在 Chrome 64 中从本地 css 文件访问 cssRules
网络选项卡的结果
So I've been reading that you can't access cssRules for external stylesheets because it runs into CORS policy issues.
I decided to take a different approach but I'm still running into issues.
My Approach:
- Download the css files on the backend and upload them to S3 Bucket
- Return back a existing link and new link for match purposes
- Delete existing
link
tag and add in a new tag that will point to my CDN - Access
document.styleSheets
- Tadaaaa (but this fails)
What I'm trying to figure out is why am I still running into issues if my CDN allows access from any origin?
export default () => {
const payload = [...document.styleSheets].filter(s => s.href).map(s => s.href);
axios.post('SOME ENDPOINT', { css: payload }).then(({ status, data: { data: newLinks } }) => {
if (status === 200) {
for (const i in newLinks) {
document.querySelector(`link[href="${newLinks[i].source}"]`).remove()
const stylesheet = document.createElement('link');
stylesheet.rel = 'stylesheet';
stylesheet.href = newLinks[i].downloaded;
document.getElementsByTagName('head')[0].appendChild(stylesheet);
}
}
}).then(() => {
let delay = 250
setTimeout(() => {
console.log('Stylesheets with Removed Links', [...document.styleSheets]);
}, delay)
}).then(() => {
console.log([...document.styleSheets])
})
}
Error on Safari SecurityError: Not allowed to access cross-origin stylesheet
I have seen this link Cannot access cssRules from local css file in Chrome 64
Result From Network Tab
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终找到了解决方案......
这一切都归功于此链接中的 Paulo Belo Uncaught DOMException: Failed to read the 'cssRules' property
stylesheet.crossOrigin = "anonymous"
解决了我的问题,让我可以访问CSS 规则。https://developer.mozilla.org/en-US/ docs/Web/HTML/Attributes/crossorigin
请注意,此修复不适用于引发此错误的现有样式表。
异常:DOMException:无法从“CSSStyleSheet”读取“cssRules”属性:无法访问 CSSStyleSheet.s 中的规则
此修复仅适用于您自己上传的工作表,或者在我的情况下适用于我的 CDN 中的工作表。
I ended up finding a solution...
All thanks to Paulo Belo from this link Uncaught DOMException: Failed to read the 'cssRules' property
stylesheet.crossOrigin = "anonymous"
solved my problem giving me access to the cssRules.https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin
Note this fix does not work with existing stylesheets that are throwing this error.
Exception: DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules at CSSStyleSheet.s
This fix only works for your own uploaded sheets or in my case the ones from my CDN.
对于 ReactJS,
crossorigin="anonymous"
( 出现跨源错误)示例:
For ReactJS,
crossorigin="anonymous"
for css (which is giving cross-origin error)Example: