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

chrome-automator

v0.2.6

Published

chrome-automator similar to NightmareJS

Downloads

39

Readme

chrome-automator

基于 chrome-remote-interface 的自动化工具。

API与 Nightmare 保持高度兼容。

Examples: (测试前请确保chrome版本大于60)

  chrome({ show: true })
  .viewport(1900, 1000)
  .useragent('Mozilla/5.0 Chrome/59.0.3071.115 Mobile Safari/537.36')
  .goto('https://www.baidu.com/')
  .wait('body')
  .insert('input#kw', 'hello world\r')
  .wait('.c-container a')
  .click('.c-container a')
  .wait('[id^="highlighter_"]')
  .screenshot('1.png')
  .evaluate(() => document.querySelectorAll('.para-title.level-3')[9].nextElementSibling.querySelector('.code').textContent)
  .pipe((code) => console.log(code))
  .url()
  .end()
  .then((url) => console.log(url))
var Nightmare = require('chrome-automator')
Nightmare.action('hello', function () {
  console.log('Get url')
  return this.evaluate_now(function () {
    return document.querySelector('#links_wrapper a.result__a').href
  })
})

var nightmare = Nightmare({ show: true })
try {
  nightmare
  .goto('https://duckduckgo.com')
  .type('#search_form_input_homepage', 'github nightmare')
  .click('#search_button_homepage')
  .wait('#zero_click_wrapper .c-info__title a')
  .hello()
  .end()
  .then(function (result) {
    console.log(result)
  })
} catch (e) {
  console.log(e)
}

命令行模式 (version >= 0.2.0)

> chrome-automator
.chrome() // 启动chrome
.goto('https://baidu.com')
.goto{ frameId: '64607.1' }
.insert('#kw', 'hello world\r')
.screenshot('1.jpg')
.title()
.end() // 关闭chrome

输出: hello world_百度搜索

API兼容列表如下:

  • [x] constructor 支持如下参数

    • [x] port
    • [x] show
    • [x] chromePath
    • [x] waitTimeout
    • [x] executionTimeout
    • [x] loadTimeout
  • [x] goto

  • [x] url

  • [x] title

  • [x] action 只支持 Promise 和 async 方式的异步写法,不支持 callback 方式

  • [x] evaluate

  • [x] click

  • [x] type

  • [x] insert

  • [x] wait

  • [x] mouseup

  • [x] mouseover

  • [x] mousedown

  • [x] check

  • [x] uncheck

  • [x] select

  • [x] scrollTo

  • [x] visible

  • [x] exists

  • [x] path

  • [x] back

  • [x] forward

  • [x] refresh

  • [x] end

  • [x] focusSelector

  • [x] blurSelector

  • [x] pdf 仅支持headless模式,设置 show: false 开启

  • [x] screenshot

  • [x] viewport

  • [x] useragent

  • [x] html

  • [x] authentication

  • [x] cookies

    • [x] set
    • [x] get
    • [x] clear
  • [x] inject

  • [x] on 暂时支持的事件与Nightmare不同

  • [x] Page.javascriptDialogOpening 弹窗事件  - [x] Console.messageAdded  旧console事件,不建议使用

  • [x] Runtime.consoleAPICalled console事件

  • [x] Network.resourceChangedPriority

  • [x] Network.requestWillBeSent

  • [x] Network.requestServedFromCache

  • [x] Network.responseReceived

  • [x] Network.dataReceived

  • [x] Network.loadingFinished

  • [x] Network.loadingFailed

  • [x] Network.webSocketWillSendHandshakeRequest

  • [x] Network.webSocketHandshakeResponseReceived

  • [x] Network.webSocketCreated

  • [x] Network.webSocketClosed

  • [x] Network.webSocketFrameReceived

  • [x] Network.webSocketFrameError

  • [x] Network.webSocketFrameSent

  • [x] Network.eventSourceMessageReceived

  • [x] Network.requestIntercepted

  • [x] Page.domContentEventFired

  • [x] Page.loadEventFired

  • [x] Page.frameAttached

  • [x] Page.frameNavigated

  • [x] Page.frameDetached

  • [x] Page.frameStartedLoading

  • [x] Page.frameStoppedLoading

  • [x] Page.frameScheduledNavigation

  • [x] Page.frameClearedScheduledNavigation

  • [x] Page.frameResized

  • [x] Page.javascriptDialogClosed

  • [x] Page.screencastFrame

  • [x] Page.screencastVisibilityChanged

  • [x] Page.interstitialShown

  • [x] Page.interstitialHidden

两种模式:

  • 非阻塞式 默认方式,建议在 goto 之前使用

  • 阻塞式,使用方式 on(eventName, fn, { detach: false })。 如想取消监听,可以 return { cancled: true } 继续接下来的流程。 例子见 test4test5

  • [x] once 只监听事件一次,监听完成后可以继续后续的动作

  • [ ] halt 暂时不支持,chrome在此场景不太适用 https://github.com/segmentio/nightmare/issues/835

  • [ ] header 目前可以使用setExtraHTTPHeaders代替部分功能

拓展功能及API:

  • [x] iframe 进入iframe,方便iframe里面的操作
  • [x] pipe 支持流程衔接,如登录流程,和 then 一样,pipe 也可以接收上个流程的返回值,建议在中间流程使用 pipe 替代 then
  • [x] 支持新窗口打开时自动跟踪,防控制跳失 注: headless模式下存在bug
  • [x] setExtraHTTPHeaders
  • [x] ignoreSWCache 忽略service worker缓存

PS:目前原有框架(Nightmare)回调写法全部去除,仅保留 Promise 写法。

Tips: 执行过程中手动进行某些操作(如打开开发者工具)可能会使用动作失效。 因为 Promise 无法取消的原因,所以在流程执行完 end 操作后node可能并不会立即退出,一般会在 30s 左右自动退出,可以缩短 loadTimeout 和 executionTimeout 解决 Promise 异步流程目前在node下还无法显示完整的错误堆栈信息,可以考虑使用 node --trace-warnings 查看,也可以使用 global.Promise = require('bluebird')解决,使用过程中记得使用 try catch 包裹执行段

LICENSE

MIT

感谢

  • Nightmare
  • chrome-remote-interface
  • lighthouse