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

node-poster-browser

v2.0.3

Published

基于puppeteer的node绘图

Downloads

67

Readme

Getting started

npm install node-poster-browser -s
yarn add node-poster-browser -s

using

const { Poster, Browser } = require('node-poster-browser')

// 初始化浏览器,在服务器启动时调用
await new Browser().init({
  CACHE_DIR: './cache',
  UPLOAD_DIR: '',
  cache: false,
  isLocalHeadless: false,
  headless: true,
  MAX_WSE: 1,
  THRESHOLD: 10,
})

// 以下为海报绘制调用步骤
const poster = new Poster({
  reqId: '', // 唯一标识,用于日志记录,内部可自动生产
  hash: '', // 文件唯一标识、文件名,内部根据 url html query 自动生成hash
  // url、 html 二选一 必传
  url: '', // 页面地址
  html: `<html>.....</html>`, // 可以为本地页面资源路径、远程页面地址、静态html模版
  ...query
})
// 海报绘制方法
await poster.createPoster()

模版语法

query = {
  text: 123,
  src: "http://xxx.png",
  show: true,
  list: [1,2,3,4]
}
  1. 解析 {{}}
<div>{{query.text}}</div> => 转为 <div>123</div>
  1. 解析 :attr=""
<img :src="query.src" /> => 转为 <img src="http://xxx.png" />
  1. 解析 v-if=""
<div v-if="!query.show">{{query.text}}</div>  => 转为 <div style="display: none;">123</div>
  1. 解析 for标签
<!-- <for variable>...</for> -->
<for query.list><div>{{query.list[i]}}</div></for>
转为
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>

Broswer Config

// Broswer config 默认参数概览
const config = {
  /**
   * 缓存配置
  */
  // 缓存本地的路径
  CACHE_DIR: './cache',
  // 上传的路径
  UPLOAD_DIR: '/images',
  // 是否准许使用缓存资源(同样的url不重新绘制)
  cache: true, //process.env.NODE_ENV === 'production',
  // 是否使用远程缓存资源
  remoteCache: false,
  // 资源缓存的最大时长 (单位: 秒)
  ttl: 5,

  /**
   * 浏览器配置
  */
  // 最多同时启动几个浏览器实例
  MAX_WSE: 1,
  // 一个浏览器实例存活的最大时长
  BROWSER_LIVE_TIME: 1000 * 60 * 30,
  // 一个浏览器实例最多启动的tab页个数
  MAX_PAGE: 4,
  // 一个tab页面闲置的最大时长
  PAGE_SLEEP_TIME: 5 * 60 * 1000,
  // 一个tab页最大的复用次数
  PAGE_COUNT: 30,
  // 浏览器启动配置
  // 默认不实用本地浏览器
  isLocalHeadless: false,
  // 默认为 centOs 本地 Chromium 地址(安装方式:yum install chromium-headless)
  executablePath: '/usr/lib64/chromium-browser/headless_shell',
  headless: true,
  launchArgs: [
    '--disable-gpu',
    '--disable-webgl',
    '--disable-dev-shm-usage',
    '--disable-setuid-sandbox',
    '--disable-extensions',
    '--disable-java',
    // '--disable-flash-3d',
    // '--disable-file-system',
    // '--disable-gl-drawing-for-tests',
    // '--disable-in-process-stack-traces',
    // '--disable-pepper-3d',
    // '--disable-javascript',
    '--app-cache-force-enabled',
    '--no-startup',
    '--no-experiments',
    '--no-default-browser-check',
    '--no-first-run',
    '--no-sandbox',
    '--no-zygote',
    '--start-maximized',
    '–-single-process'
  ],
  /**
   * 任务节流控制配置
  */
  // 是否启用任务节流控制
  limit: true,
  // 最大同时运行任务数量阙值
  THRESHOLD: 10,
  // 任务池最大长度
  TASKLEN: 850,
  /**
   * 其他配置
  */
  // token 失效时间 moment参数 TODO: 暂时未使用
  // exp: [2, 'hours'],
  /**
   * 日志配置
  */
  // 是否打印任务执行日志 log输出等级, 0为不输出
  logLevel: ['log', 'info', 'warn', 'error'],
  log: {
    default: './logs/combined.log',
    error: './logs/error.log',
    info: './logs/info.log',
  }
}
new Browser().init(config)

Installation

Note: When you install Puppeteer, it downloads a recent version of Chromium (~170MB Mac, ~282MB Linux, ~280MB Win) that is guaranteed to work with the API. To skip the download, or to download a different browser, see Environment variables.

puppeteer-core

Since version 1.7.0 we publish the puppeteer-core package, a version of Puppeteer that doesn't download any browser by default.

npm i puppeteer-core
# or "yarn add puppeteer-core"

puppeteer-core is intended to be a lightweight version of Puppeteer for launching an existing browser installation or for connecting to a remote one. Be sure that the version of puppeteer-core you install is compatible with the browser you intend to connect to.

See puppeteer vs puppeteer-core.

example.js

const puppeteer = require('puppeteer');
(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});
  await browser.close();
})();