jsdomで取得したhtmlで読み込まれている外部cssをjavascriptで取得したいのですが、うまく行かずに困っております。。。
※細かく言うと、htmlのcssRulesを取得して操作したいです。
環境
- node:11.10.1
- typescript:3.6.3
- jsdom:15.1.1
- axios:0.19.0
ドキュメントを日本語訳して見た感じですと、new JSDOM した際のオプションに resources: 'usable' を指定すると外部css(link要素で指定しているcss)も取得できると書いてあるのですが、現状はhtmlのsytle要素に直書きしている内容しか取得できていません。。。
以下typescriptでサンプルを記述しています、アドバイス頂けますと大変ありがたいですm(_ _)m
typescript
1import axios, { AxiosResponse } from 'axios' 2import { JSDOM } from 'jsdom' 3 4const targetUrl = new URL('対象のURL'); 5const auth = undefined; 6 7const main = async () => { 8 9 let dom = await axios({ 10 method: 'GET', 11 url: targetUrl.href, 12 responseType: 'document', 13 auth: auth 14 }).then((res) => { 15 return new JSDOM(res.data, { 16 resources: 'usable' 17 }).window.document; 18 }); 19 20 let styleSheet = <CSSStyleSheet>(dom.styleSheets[0]); 21 console.table(styleSheet); // undefined 22}; 23 24main() 25 .then(() => { 26 console.log('処理が完了しました。'); 27 }).catch(err => { 28 console.log('エラーが発生しました。'); 29 console.log(err); 30 });
あなたの回答
tips
プレビュー