Skip to content

Commit fa34aa8

Browse files
committed
support array of url
1 parent 33d054a commit fa34aa8

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const [{css}] = await fetchCss([{crx: "hlepfoohegkhhmjieoechaddaejaokhf"}]);
2222

2323
- `sources`: *Array* Array of source objects
2424
- `source`: *Object*
25-
- `url`: *string* An absolute URL pointing to either a website or directly to a CSS or JS file (to extract inlined CSS strings from)
25+
- `url`: *string* or *Array* An absolute URL pointing to either a website or directly to a CSS or JS file (to extract inlined CSS strings from)
2626
- `fetchOpts`: *Object* Options passed to [fetch](https://github.com/npm/make-fetch-happen#fetch)
2727
- `crx`: *string* A Chrome extension id
2828
- `contentScriptsOnly`: *boolean* Whether to pull only content scripts from a extension. Default: `false`

index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,24 @@ export default async function fetchCss(sources) { // eslint-disable-line import/
185185
const sourceResponses = await Promise.all(sources.map(source => {
186186
if (!source.url) return null;
187187
const {pathname} = new URL(source.url);
188-
return pathname.endsWith(".css") || pathname.endsWith(".js") ? null : doFetch(source.url, source.fetchOpts);
188+
if (pathname.endsWith(".css") || pathname.endsWith(".js")) return null;
189+
190+
if (Array.isArray(source.url)) {
191+
return source.url.map(url => doFetch(url, source.fetchOpts));
192+
} else {
193+
return doFetch(source.url, source.fetchOpts);
194+
}
189195
}));
190196

191197
for (const [index, res] of Object.entries(sourceResponses)) {
192198
const source = sources[index];
193199
if (res) {
194-
validateStatus(res, source.url, source.strict);
195-
const [styleUrls, styleTags] = await extract(res);
196-
source.urls = styleUrls;
197-
source.styleTags = styleTags;
200+
for (const r of Array.isArray(res) ? res : [res]) {
201+
validateStatus(r, source.url, source.strict);
202+
const [styleUrls, styleTags] = await extract(r);
203+
source.urls.push(...styleUrls);
204+
source.styleTags.push(...styleTags);
205+
}
198206
} else if (source.url) {
199207
source.urls = [source.url];
200208
}

0 commit comments

Comments
 (0)