Skip to content

Commit 5c1cbc9

Browse files
authored
🔥 replace browserOverride with customBrowser
🔥 removed the browserOverride option 🔥 removed the debug option 💎 added the customBrowser option closes #4
2 parents 5692c87 + 9c384a4 commit 5c1cbc9

File tree

5 files changed

+31
-141
lines changed

5 files changed

+31
-141
lines changed

readme.md

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ the relevant
4141

4242
```sh
4343
npm install extract-css-core
44-
45-
# Or with Yarn
44+
# or
4645
yarn add extract-css-core
4746
```
4847

@@ -51,7 +50,7 @@ yarn add extract-css-core
5150
```js
5251
const extractCss = require('extract-css-core')
5352

54-
extractCss('http://www.projectwallace.com').then(css => console.log(css))
53+
const css = await extractCss('http://www.projectwallace.com')
5554
```
5655

5756
## API
@@ -67,15 +66,6 @@ Type: `Object`
6766

6867
Default: `null`
6968

70-
#### debug
71-
72-
Type: `Boolean`
73-
74-
Default: `false`
75-
76-
Set to `true` if you want a Chromium window to open as it works to get all the
77-
CSS from the page.
78-
7969
#### waitUntil
8070

8171
Type: `String`
@@ -85,24 +75,21 @@ Default: `networkidle2`
8575
Can be any value as provided by the
8676
[Puppeteer docs](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagegotourl-options).
8777

88-
#### browserOverride
78+
#### customBrowser
8979

90-
Type: `Object`
80+
Type: `Browser`
9181

9282
Default: `null`
9383

94-
An object consisting of the following fields:
95-
96-
- `executablePath`: the path to a Chromium binary
97-
- `puppeteer`: a Puppeteer instance
98-
- `args`: arguments to start Chromium with
99-
100-
See the `test` directory for implementation examples. This option exists
101-
primarily for use with [extract-css.now.sh](https://extract-css.now.sh)
84+
A
85+
[Puppeteer Browser](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-browser)
86+
instance. See the `test` directory for implementation examples. This option
87+
exists primarily for use with
88+
[extract-css](https://github.com/bartveneman/extract-css)
10289

10390
## Related
10491

105-
- [extract-css lambda](https://github.com/bartveneman/extract-css) - Extract CSS
92+
- [extract-css](https://github.com/bartveneman/extract-css) - Extract CSS
10693
running as a serverless function
10794
- [get-css](https://github.com/cssstats/cssstats/tree/master/packages/get-css) -
10895
The original get-css

src/index.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const puppeteer = require('puppeteer')
2-
const {validateBrowserOverride} = require('./validate')
32

43
function InvalidUrlError({url, statusCode, statusText}) {
54
this.name = 'InvalidUrlError'
@@ -10,27 +9,19 @@ InvalidUrlError.prototype = Error.prototype
109

1110
module.exports = async (
1211
url,
13-
{debug = false, waitUntil = 'networkidle2', browserOverride = null} = {}
12+
{waitUntil = 'networkidle2', customBrowser = null} = {}
1413
) => {
15-
// Basic validation for browserOverride
16-
if (browserOverride !== null) {
17-
validateBrowserOverride(browserOverride)
18-
}
19-
20-
// Setup the minimal browser options that we need to launch
21-
const browserOptions = {
22-
headless: debug !== true,
23-
executablePath: browserOverride
24-
? browserOverride.executablePath
25-
: puppeteer.executablePath(),
26-
args: browserOverride ? browserOverride.args : []
14+
if (
15+
customBrowser !== null &&
16+
(!customBrowser.isConnected || !customBrowser.isConnected())
17+
) {
18+
return Promise.reject(
19+
new TypeError('The `customBrowser` option is invalid')
20+
)
2721
}
2822

2923
// Setup a browser instance
30-
const browser = await (
31-
(browserOverride && browserOverride.puppeteer) ||
32-
puppeteer
33-
).launch(browserOptions)
24+
const browser = customBrowser || (await puppeteer.launch())
3425

3526
// Create a new page and navigate to it
3627
const page = await browser.newPage()

src/validate.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

test/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ test('it combines server generated <link> and <style> tags with client side crea
103103
const actual = await extractCss(server.url + path)
104104

105105
t.snapshot(actual)
106+
t.true(actual.includes('counter-increment: 2;'))
106107
})
107108

108109
test('it rejects if the url has an HTTP error status', async t => {
@@ -125,13 +126,18 @@ test('it accepts a browser override for usage with other browsers', async t => {
125126
server.get(path, (req, res) => {
126127
res.send(kitchenSinkExample)
127128
})
128-
const actual = await extractCss(server.url + path, {
129-
browserOverride: {
130-
executablePath: chromium.path,
131-
puppeteer: puppeteerCore,
132-
args: []
133-
}
129+
const customBrowser = await puppeteerCore.launch({
130+
executablePath: chromium.path
134131
})
132+
const actual = await extractCss(server.url + path, {customBrowser})
135133

136134
t.snapshot(actual)
135+
t.true(actual.includes('counter-increment: 2;'))
136+
})
137+
138+
test('it rejects on an invalid customBrowser option', async t => {
139+
const path = '/browser-override'
140+
await t.throwsAsync(extractCss(server.url + path, {customBrowser: {}}), {
141+
message: 'The `customBrowser` option is invalid'
142+
})
137143
})

test/validate.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)