Skip to content

Commit

Permalink
Version 1.0.3
Browse files Browse the repository at this point in the history
新增: 自定义字体
  • Loading branch information
Lete114 committed Mar 6, 2022
1 parent 411119c commit ba4b49f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

## 快速开始

### 免费部署

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/Lete114/WebStack-Screenshot/tree/Vercel)

### NPM 安装

初始化`npm`项目,并且安装`webstack-screenshot`
Expand Down Expand Up @@ -72,6 +76,7 @@ npm run start:hot
| 属性 | 默认值 | 类型 | 描述 |
| --------- | ------ | ------- | ------------------------------------------------------------- |
| url | | String | 请求的网站 URL 地址,如果输入的是域名会自动拼接`http://` |
| font | | String | 如果指定的截图网站出现乱码,你可通过该参数指定字体`url`地址 |
| viewport | | Int | 截图 100 宽 200 高,格式`100x200`(需添加`fullPage=false`) |
| isMobile | false | Boolean | 是否是手机端 |
| await | 0 | Int | 页面渲染完成后等待,`0`表示不等待(单位毫秒) |
Expand Down
5 changes: 5 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Just some simple screenshot operations, if you have any needs, or want to partic

## Quick Start

### Free Deployment

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/Lete114/WebStack-Screenshot/tree/Vercel)

### NPM Installation

Initialize the `npm` project and install the `webstack-screenshot` library
Expand Down Expand Up @@ -71,6 +75,7 @@ Request Method: GET | POST
| Properties | Default | Type | Description |
| ---------- | ------- | ------- | ---------------------------------------------------------------------------------------------------------------------- |
| url | | String | The URL address of the requested website, if you enter a domain name it will be automatically spelled out as `http://` |
| font | | String | If the specified screenshot site appears garbled, you can specify the font `url` address with this parameter |
| viewport | | Int | Screenshot 100 wide by 200 high, format `100x200` (need to add `fullPage: false`) |
| isMobile | false | Boolean | Whether it is mobile |
| await | 0 | Int | How long do I wait after the page is rendered,`0` means no waiting (milliseconds) |
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webstack-screenshot",
"version": "1.0.2",
"version": "1.0.3",
"description": "网站截图 API | Website Screenshot API ",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -30,7 +30,7 @@
"url": "git+https://github.com/Lete114/WebStack-Screenshot.git"
},
"dependencies": {
"body-data": "^1.0.2",
"body-data": "^1.0.3",
"chrome-aws-lambda": "^10.1.0",
"puppeteer": "^13.4.1",
"puppeteer-core": "^13.4.1"
Expand Down
12 changes: 9 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ module.exports = async (req, res) => {
if (req.url === '/favicon.ico') return res.end()
const data = await bodyData(req)

const projectUrl =
'https://github.com/Lete114/WebStack-Screenshot#%E5%B1%9E%E6%80%A7'
if (!data.url) {
return res.end(JSON.stringify({ msg: 'URL not detected' }))
return res.end(
JSON.stringify({
msg: 'URL not detected , Using parameters: ' + projectUrl
})
)
}

// 判断是服务器(Server)还是无服务器(ServerLess),决定使用Chromium
const launchOpt = await launch()
const launchOpt = await launch(data.font)

browser = await puppeteer.launch(launchOpt)

Expand Down Expand Up @@ -45,7 +51,7 @@ module.exports = async (req, res) => {
// 关闭浏览器
// 每次关闭浏览器后,下次再次使用会重新启动浏览器,消耗性能(消耗几百毫秒的启动时间)
// 可直接关闭标签页来提高性能 `await page.close()`
// 或是保留标签页到缓存中,如果一直被请求同一个url那么直接使用缓存里的标签页(会增加服务器内存消耗,但能道极高的请求响应速度)
// 或是保留标签页到缓存中,如果一直被请求同一个url那么直接使用缓存里的标签页(会增加服务器内存消耗,但能得到极高的请求响应速度)
// 如果你想优化,欢迎你PR哦~(其实我就是懒才直接关闭浏览器的)
await browser.close()

Expand Down
19 changes: 15 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,35 @@ function isBoolean(value) {
return value === 'true' || value === true ? true : false
}

function isHttp(url) {
return /^https?:\/\//.test(url)
}

module.exports = {
async launch() {
// 设置字体
async launch(fontUrl) {
// 设置字体: 文泉驿宽微米黑
const fontPath = join(__dirname, '../font/WenQuanDengKuanWeiMiHei-1.ttf')
await chromium.font(fontPath)

// 判断是否是url字体
const font = isHttp(fontUrl) ? fontUrl : fontPath

// 使用字体
await chromium.font(font)

// lambda (ServerLess) 配置
const chromiumOptions = {
args: chromium.args,
executablePath: await chromium.executablePath,
headless: chromium.headless
}
// 判断是服务器(Server)还是无服务器(ServerLess)
return process.env.PUPPETEER_SERVER ? {} : chromiumOptions
},
goto(data) {
const options = {}

// 是否以http协议开头
data.url = /^https?:\/\//.test(data.url) ? data.url : 'http://' + data.url
data.url = isHttp(data.url) ? data.url : 'http://' + data.url

// 超时,默认30s
if (!isNaN(data.timeout)) options.timeout = Math.abs(data.timeout) ?? 30000
Expand Down

0 comments on commit ba4b49f

Please sign in to comment.