I am trying to detect (and then change) stylesheet in any loaded page.
I try the folowing code:
// from https://developer.mozilla.org/en-US/docs/Web/API/StyleSheetList var allCSS = [].slice.call(document.styleSheets) .reduce(function (prev, styleSheet) { if (styleSheet.cssRules) { return prev + [].slice.call(styleSheet.cssRules) .reduce(function (prev, cssRule) { return prev + cssRule.cssText; }, ''); } else { return prev; } }, ''); console.log(allCSS);
When I run this code on this page for example, I get a few stylesheets in the console.
However this is definitely not all the styles, it is infact just a small portion of it.
For example in the debugger I see that the page loads all.css
from 'cdn.sstatic.net' with many styles, none of them are shown.
What am I doing wrong ? and how can I get ALL stylesheets ?
Thx!
In some browsers, if a stylesheet is loaded from a different domain,
calling cssRules
results in SecurityError.
document.styleSheets
The cssRules attribute must follow these steps:
如果需要获取和更改在document
加载的.css
文件,则可以请求在DevTools
查看的文件,从document
删除所有现有样式表,对响应文本进行调整,将调整后的文本作为.css
附加到document
或在<style>
元素。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.