├── .gitignore ├── .eslintrc.json ├── package.json ├── example.js ├── index.js ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | lib/ 4 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["eslint:recommended", "prettier"], 3 | "env": { 4 | "node": true, 5 | "es6": true 6 | }, 7 | "rules": { 8 | "eqeqeq": [2, "smart"], 9 | "no-caller": 2, 10 | "dot-notation": 2, 11 | "no-var": 2, 12 | "prefer-const": 2, 13 | "prefer-arrow-callback": [2, { "allowNamedFunctions": true }], 14 | "arrow-body-style": [2, "as-needed"], 15 | "object-shorthand": 2, 16 | "prefer-template": 2, 17 | "one-var": [2, "never"], 18 | "prefer-destructuring": [2, { "object": true }], 19 | "capitalized-comments": 2, 20 | "multiline-comment-style": [2, "starred-block"], 21 | "spaced-comment": 2, 22 | "yoda": [2, "never"], 23 | "curly": [2, "multi-line"], 24 | "no-else-return": 2 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cornet", 3 | "author": "Felix Boehm ", 4 | "version": "0.1.5", 5 | "description": "transform streaming html using css selectors", 6 | "main": "index.js", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/fb55/node-cornet" 10 | }, 11 | "keywords": [ 12 | "trumpet", 13 | "html", 14 | "css", 15 | "selector", 16 | "htmlparser2", 17 | "handler" 18 | ], 19 | "dependencies": { 20 | "css-select": "^5.1.0", 21 | "domhandler": "~2.4.0", 22 | "domutils": "~1.7.0" 23 | }, 24 | "devDependencies": { 25 | "eslint": "^7.6.0", 26 | "eslint-config-prettier": "^6.11.0", 27 | "prettier": "^2.0.5" 28 | }, 29 | "prettier": { 30 | "tabWidth": 4, 31 | "proseWrap": "always" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | const { WritableStream: Parser } = require("htmlparser2"); 2 | const Cornet = require("./"); 3 | const minreq = require("minreq"); 4 | const $ = require("cheerio"); 5 | 6 | const cornet = new Cornet(); 7 | 8 | minreq.get("http://github.com/fb55").pipe(new Parser(cornet)); 9 | 10 | cornet.remove("script"); // Remove all scripts 11 | 12 | // Show all repos 13 | cornet.select(".repo_list", (elem) => { 14 | $(elem) 15 | .find("h3") 16 | .each((i, el) => 17 | console.log("repo %d: %s", i + 1, $(el).text().trim()) 18 | ); 19 | }); 20 | 21 | // Does the same 22 | let i = 0; 23 | cornet.select(".repo_list h3", (elem) => { 24 | console.log("repo %d: %s", ++i, $(elem).text().trim()); 25 | }); 26 | 27 | // Sometimes, you only want to get a single element 28 | const onTitle = cornet.select("title", (title) => { 29 | console.log("Page title:", $(title).text().trim()); 30 | cornet.removeLister("element", onTitle); 31 | }); 32 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const DomHandler = require("domhandler"); 2 | const DomUtils = require("domutils"); 3 | const { compile } = require("css-select"); 4 | const { EventEmitter } = require("events"); 5 | 6 | class Handler extends EventEmitter { 7 | constructor(options) { 8 | super(); 9 | DomHandler.call( 10 | this, 11 | (e, dom) => this.emit("dom", dom), 12 | options, 13 | (elem) => this.emit("element", elem) 14 | ); 15 | } 16 | select(selector, cb) { 17 | if (typeof selector === "string") { 18 | selector = compile(selector); 19 | } 20 | function onElem(elem) { 21 | if (selector(elem)) cb(elem); 22 | } 23 | this.on("element", onElem); 24 | return onElem; 25 | } 26 | remove(selector) { 27 | return this.select(selector, DomUtils.removeElement); 28 | } 29 | } 30 | 31 | Object.getOwnPropertyNames(DomHandler.prototype).forEach((name) => { 32 | Handler.prototype[name] = DomHandler.prototype[name]; 33 | }); 34 | 35 | module.exports = Handler; 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) Felix Böhm 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS, 11 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## About 2 | 3 | > The **cornet** is a brass instrument very similar to the trumpet, 4 | > distinguished by its conical bore, compact shape, and mellower tone quality. - 5 | > [Wikipedia](http://en.wikipedia.org/wiki/Cornet) 6 | 7 | This project is demonstrating how to use a couple of my libraries to replace 8 | [`substack/node-trumpet`](https://github.com/substack/node-trumpet) in just a 9 | couple of LOC. 10 | 11 | Even better, there are some advantages over `trumpet`: 12 | 13 | - The ammount of usable CSS selectors is increased dramatically thanks to 14 | [`fb55/css-select`](https://github.com/fb55/css-select). 15 | - `cornet` works as a handler for 16 | [`fb55/htmlparser2`](https://github.com/fb55/htmlparser2), the probably 17 | fastest HTML parser currently available for node. And it's much less strict 18 | than the `sax` module used by `trumpet`. 19 | - By using the great 20 | [`cheeriojs/cheerio`](https://github.com/cheeriojs/cheerio) module, you can 21 | do everything with your document that would be possible with jQuery. 22 | 23 | _Please note that callbacks are fired as soon as an element was retrieved. That 24 | means that no content past the element will be available, so cheerio won't find 25 | anything, and, as the element is at this time the last child of it's parent, 26 | selectors like `:nth-last-child` won't work as expected._ 27 | 28 | ## Install 29 | 30 | npm install cornet 31 | 32 | ## Example 33 | 34 | ```js 35 | const Parser = require("htmlparser2").WritableStream; 36 | const Cornet = require("cornet"); 37 | const minreq = require("minreq"); 38 | const $ = require("cheerio"); 39 | 40 | const cornet = new Cornet(); 41 | 42 | minreq.get("http://github.com/fb55").pipe(new Parser(cornet)); 43 | 44 | cornet.remove("script"); //remove all scripts 45 | 46 | //show all repos 47 | cornet.select(".repo_list", function (elem) { 48 | $(elem) 49 | .find("h3") 50 | .each(function (i) { 51 | console.log("repo %d: %s", i + 1, $(this).text().trim()); 52 | }); 53 | }); 54 | 55 | //does the same 56 | const i = 0; 57 | cornet.select(".repo_list h3", function (elem) { 58 | console.log("repo %d: %s", ++i, $(elem).text().trim()); 59 | }); 60 | 61 | //sometimes, you only want to get a single element 62 | const onTitle = cornet.select("title", function (title) { 63 | console.log("Page title:", $(title).text().trim()); 64 | cornet.removeLister("element", onTitle); 65 | }); 66 | ``` 67 | 68 | ## API 69 | 70 | #### `cornet(options)` 71 | 72 | The constructor. `options` are the same you can pass to 73 | [`fb55/DomHandler`](https://github.com/fb55/DomHandler). 74 | 75 | It's an `EventEmitter` that emits two events: 76 | 77 | - `element` is emitted whenever an element was added to the DOM. 78 | - `dom` is emitted when the DOM is complete. 79 | 80 | #### `cornet#select(selector | fn, cb)` 81 | 82 | Calls the callback when the selector is matched or a passed function returns 83 | `true` (or any value that evaluates to true). 84 | 85 | Internally, listenes for any `element` event and checks then if the selector is 86 | matched. 87 | 88 | Returns the listening function, so you can remove it afterwards (as shown in the 89 | example above). 90 | 91 | #### `cornet#remove(selector | fn)` 92 | 93 | Removes all elements that match the selector. Also returns the listener. 94 | --------------------------------------------------------------------------------