Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

browser-cli

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

browser-cli - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

.npmignore

109

index.js
console.log('######');
var util = require('util');
var request = require('request');
var htmlparser = require('htmlparser');
var EventEmitter = require('events').EventEmitter;
var repl = require('repl');
setTimeout(function() {
console.log('\033[2J\033[0f');
}, 2000);
function clear() {
console.log('\033[2J\033[0f');
}
function getStatusString(status) {
switch(status) {
case -1: return 'error';
case 0: return 'none';
case 1: return 'request';
case 2: return 'parse';
case 3: return 'render';
}
}
function Window(option) {
this.option = option;
this.status = 0;
this.tagid = 0;
this.document = {};
this.trace = [];
this.cursor = this.document;
// Just for debugging
this.rli = repl.start('>');
}
util.inherits(Window, EventEmitter);
Window.prototype.request = function(url) {
var self = this;
this.url = url;
this.status = 1;
this._render();
request(this.url, function(error, response, body) {
self.status = response.statusCode === 200 ? 2 : -1;
self._render()._parse(body);
console.log(self.document);
self.title = self.document.html.head.title.value.slice(0, 50)+'...';
self.body = self.document.html.body;
self._render();
console.log(JSON.stringify(self.document, null,2));
});
};
Window.prototype._parse = function(content) {
var self = this;
var handler = new htmlparser.DefaultHandler(function(err, dom) {
dom.forEach(parseDomNode);
function parseDomNode(node) {
// break branch
if (node.type !== 'tag') {
if (node.type === 'text') self.cursor.value = node.data;
return;
}
if (!/html|head|title|body/i.test(node.name)) {
node.name = util.format('%s_%d', node.name, self.tagid++);
}
self.cursor[node.name] = {};
if (node.children) {
self.trace.push(self.cursor);
self.cursor = self.cursor[node.name];
if (typeof node.children.forEach === 'function')
node.children.forEach(parseDomNode);
else
parseDomNode(node.children);
self.cursor = self.trace.pop();
}
}
});
var parser = new htmlparser.Parser(handler);
parser.parseComplete(content);
self.status = 3;
};
Window.prototype.render =
Window.prototype._render = function() {
clear();
switch (this.status) {
case -1:
case 0:
case 1:
console.log(util.format(' \033[90murl: %s\033[0m', this.url));
break;
case 2:
console.log(util.format(' \033[36murl: %s\033[0m', this.url));
break;
case 3:
console.log(util.format(' \033[36murl: %s\033[0m', this.url));
console.log(util.format(' \033[36mtitle:\033[0m \033[90m%s\033[0m', this.title));
console.log(util.format(' \033[90m%s\033[0m', '=============================='));
break;
default:
console.log('Null');
}
return this;
};
exports.Window = Window;
{
"name": "browser-cli",
"version": "0.0.1",
"version": "0.0.2",
"description": "html render in your shell",
"main": "index.js",
"scripts": {
"test": "tape test/*.js"
"test": "tape test/*.js",
"demo": "node demo"
},

@@ -17,2 +18,9 @@ "repository": {

],
"dependencies": {
"request": "*",
"htmlparser": "*"
},
"devDependencies": {
"tape": "*"
},
"author": "Yorkie Neil <yorkiefixer@gmail.com>",

@@ -19,0 +27,0 @@ "license": "MIT",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc