npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

phobetor

v0.3.3

Published

Convenience abstraction of Nightmare.js

Downloads

12

Readme

Phobetor

Browser emulation library build with Nightmare.js


Nightmare.js is great library, but you cannot straightforward pass functions as arguments to the evaluation context.

Phobetor uses eval to run those functions in browser's context and give you back the result.

Example use

Reddit login

// yarn add phobetor
const browser = require("phobetor");
(async () => {
  browser.init({show:true})
  await browser.get("https://www.reddit.com/")
  await browser.wait({condition:1000})
  await browser.type({
    selector:"input[name='user']",
    payload:"foo"
  })
  await browser.type({
    selector:"input[name='passwd']",
    payload:"bar"
  })
  const submitButtonSelector = ".submit button[type=submit]"
  await browser.click({
    selector: submitButtonSelector
  })
  await browser.sleep(2000)
  await browser.close()
})()

This example is part of Phobetor's showcase project Reddit voter, which logs into Reddit with your credentials, redirect to declared subreddit and then vote randomly.

Naming

Phobetor is Greek god of nightmares

Phobetor

API

Two most important functions in 'Phobetor' are 'getElements' and 'getSingleElement'. Most of the other methods are build using those two functions.

getElements

Typing

getElements({
  constants?: any,
  evaluate?: Function,
  filter?: Function<Boolean>,
  map?: Function,
  selector: String
}): Promise
  • selector(required)

Elements query selector compatible with document.querySelectorAll

  • filter

Boolean returning function. You can use it to filter the result of document.querySelectorAll(selector).

If filter fails to return true for at least one element, you will receive null as result.

  • map

Function that will map over the returned elements from filter

  • evaluate

Function that receives result of map as argument. What evaluate returns is the actual return value of getElements

  • constants

You can use constants to pass values to browser context

Example

const browser = require("phobetor");
browser.init({show:true})
const constants = "placeholder"
const result = await browser.getElements({
  constants: {limit: 100},
  selector:".foo",
  filter: element => element.innerHTML.length > constants.limit,
  map: element => element.querySelector("p").textContent,
  evaluate: elements => elements.join("\n")
})

Notice that in order to use constants in our functions, you need to have it in your context, when you declare evaluate-like functions.

For convenience this method has alias '$$' so you can use it also like that:

const result = await browser.$$({
  ...
})

getSingleElement

Typing

getSingleElement({
  constants?: any,
  evaluate?: Function,
  filter?: Function,
  selector: String
}): Promise
  • selector*(required)

Element query selector compatible with document.querySelector

  • filter

Boolean returning function. You can use it to filter the result of document.querySelector(selector)

If filter return false, you will receive null as result.

  • evaluate

Function that receives result of document.querySelector(selector) as argument. What evaluate returns is the actual return value of getSingleElement

  • constants

You can use constants to pass values to browser context

Example

const constants = "placeholder"
const result = await browser.getSingleElement({
  constants: "baz",
  selector:".foo",
  filter: element => element.innerHTML.includes(constants),
  evaluate: element => element.textContent
})

As you may expect, when filter is evaluated, it will replace constants with "baz" not "placeholder".

For convenience this method has alias '$' so you can use it also like that:

const result = await browser.$({
  ...
})

getNightmareInstance(): Object

Return the same Nightmare.js instance as the library uses itself.

init(options: Object)

options are used to create Nightmare.js instance

Check requirements at Nightmare.js's page

get(url: String): Promise

Same as Nightmare.js.goto

Check Nightmare.js's desription for goto

wait

Typing

wait({
  condition: String|Function|Number,
  conditionArguments?: any,
  timeout?: Number
}): Promise
  • condition is passed to Nightmare.js's wait

  • If condition is a function, it is passed to Nightmare.js's wait with conditionArguments as argument.

  • timeout is delay which race with condition. Thus even if condition fails, you can proceed as nothing happened. Default value is 5000ms

isElementPresent

isElementPresent({selector:String}):Promise<Boolean>

sleep

sleep(ms: Number): Promise

setTimeout as a promise

delay

delay(ms: Number): Promise

setTimeout as a promise