2.0.3 • Published 3 years ago

node-poster-browser v2.0.3

Weekly downloads
74
License
-
Repository
-
Last release
3 years ago

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();
})();
2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.34

3 years ago

0.0.35

3 years ago

0.0.36

3 years ago

0.0.33

3 years ago

0.0.32

3 years ago

0.0.21

3 years ago

0.0.19

3 years ago

0.0.31

3 years ago

0.0.1

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

1.0.9

3 years ago

0.0.22

3 years ago

0.0.23

3 years ago

0.0.24

3 years ago

0.0.25

3 years ago

0.0.30

3 years ago

0.0.26

3 years ago

0.0.27

3 years ago

0.0.28

3 years ago

0.0.29

3 years ago

0.0.20

3 years ago

0.0.14

3 years ago

0.0.15

3 years ago

0.0.16

3 years ago

0.0.17

3 years ago

0.0.18

3 years ago

0.0.13

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.5

3 years ago

0.0.6

3 years ago

0.0.4

3 years ago