auto-chrome
Advanced tools
Comparing version 0.6.0 to 0.6.1
@@ -41,3 +41,5 @@ "use strict" | ||
async down(options = {}) { | ||
this.button = (options.button || 'left'); | ||
await this.send('Input.dispatchMouseEvent', { | ||
@@ -50,2 +52,3 @@ type: 'mousePressed', | ||
}); | ||
} | ||
@@ -60,2 +63,3 @@ /** | ||
async move(x, y, options = {}) { | ||
const fromX = this.x, fromY = this.y; | ||
@@ -73,2 +77,3 @@ this.x = x; | ||
} | ||
} | ||
@@ -89,3 +94,3 @@ /** | ||
/** | ||
* 相对于窗口可视区滚动至指定坐标,目前仅支持纵向滚动 | ||
* 相对于窗口可视区滚动至指定坐标,仅支持垂直滚动 | ||
* @param {number} x 相对于窗口的横向偏移量 | ||
@@ -98,2 +103,3 @@ * @param {number} y 相对于窗口的纵向偏移量 | ||
let count = y / step | ||
for (let i = 0; i <= count; i++) { | ||
@@ -199,3 +205,3 @@ await this.send('Input.dispatchMouseEvent', { | ||
*/ | ||
static async windowInfo() { | ||
async windowInfo() { | ||
@@ -219,53 +225,48 @@ let func = (function () { | ||
/** | ||
* 通过touch滚动至页面指定坐标 | ||
* @param {Number} x 横坐标 | ||
* @param {Number} y 纵坐标 | ||
* 通过touch滚动指定像素 | ||
* @param {Number} x 横坐滑动距离 | ||
* @param {Number} y 纵坐滑动距离 | ||
* @param {Object} options 选项 | ||
*/ | ||
async scroll(x, y, options = {}) { | ||
async scroll(x, y) { | ||
let { interval = 2000 } = options | ||
// 获取当前浏览器滚动条位置 | ||
let { scrollX, scrollY, innerWidth, innerHeight } = await this.windowInfo(); | ||
let result = await this.windowInfo() | ||
let totalX = x - scrollX | ||
let totalY = y - scrollY | ||
let { innerWidth, innerHeight } = result | ||
let centerX = Math.round(innerWidth / 2) | ||
let centerY = Math.round(innerHeight / 2) | ||
if (totalY > centerY) { | ||
totalY -= centerY | ||
} else { | ||
return | ||
} | ||
let plusX = 0 | ||
let plusY = 0 | ||
// 分多次发送滑动事件 | ||
while (totalY > plusY) { | ||
while (y > 0) { | ||
// 模拟随机坐标,让每次的滑动轨迹都不一样 | ||
let startX = Math.round(innerWidth * (0.3 + Math.random() * 0.4)) | ||
let startY = Math.round(innerHeight * (0.6 + Math.random() * 0.2)) | ||
let startY = Math.round(innerHeight * (0.8 + Math.random() * 0.15)) | ||
let endX = Math.round(startX + Math.random() * 0.1) | ||
let endY = Math.round(innerHeight * (0.2 + Math.random() * 0.2)) | ||
let endY = Math.round(innerHeight * (0.1 + Math.random() * 0.15)) | ||
plusX += startX - endX | ||
plusY += startY - endY | ||
let moveY = startY - endY | ||
// 末端补齐 | ||
if (totalY < plusY) { | ||
endY = startX + (plusY - totalY) | ||
if (y > moveY) { | ||
y -= moveY | ||
} else { | ||
endY = startY - y | ||
y = 0 | ||
} | ||
let start = { x: startX, y: startY } | ||
let end = { x: endX, y: endY } | ||
let start = { | ||
x: startX, | ||
y: startY | ||
} | ||
await this.slide({ start, end }); | ||
let end = { | ||
x: endX, | ||
y: endY | ||
} | ||
await sleep(interval) | ||
await this.slide({ start, end }) | ||
await sleep(600) | ||
} | ||
@@ -272,0 +273,0 @@ |
@@ -174,5 +174,5 @@ "use strict" | ||
args: [{ value: name }, { value }], | ||
func(element, name, value) { | ||
func: (element, name, value) => { | ||
element[name] = value | ||
}, | ||
} | ||
}) | ||
@@ -223,9 +223,22 @@ | ||
let { x, y, width, height } = await this.getBoundingRect() | ||
const result = await this.getBoundingRect() | ||
let { x, y, width, height } = result | ||
// 定位到元素中心 | ||
x = x + width / 2 | ||
y = y + height / 2 | ||
await this.evaluate({ | ||
func: (element) => { | ||
element.style.border = "1px solid #ed0000" | ||
}, | ||
}) | ||
await sleep(500) | ||
await this.page.clicker.click(x, y) | ||
return result | ||
} | ||
@@ -238,6 +251,8 @@ | ||
let { x, y, height, innerHeight } = await this.getBoundingRect() | ||
let result = await this.getBoundingRect() | ||
let centreY = (height + innerHeight) / 2 | ||
let { x, y, height, innerHeight } = result | ||
let centreY = (innerHeight - height) / 2 | ||
y = y - centreY | ||
@@ -247,2 +262,4 @@ | ||
return result | ||
} | ||
@@ -249,0 +266,0 @@ |
"use strict" | ||
const { assert } = require('./helper'); | ||
const keyDefinitions = require('./USKeyboard'); | ||
const { assert, sleep } = require('./helper'); | ||
@@ -129,2 +129,3 @@ class Keyboard { | ||
async sendCharacter(char) { | ||
await this.send('Input.dispatchKeyEvent', { | ||
@@ -137,2 +138,3 @@ type: 'char', | ||
}); | ||
} | ||
@@ -144,14 +146,20 @@ | ||
*/ | ||
async type(text, options = {}) { | ||
let { delay = 80 } = options | ||
async type(text) { | ||
let chars = [] | ||
for (const char of text) { | ||
if (keyDefinitions[char]) { | ||
await this.press(char, { delay: 20 }); | ||
if (chars.length) { | ||
await this.sendCharacter(chars.join('')); | ||
chars = [] | ||
} | ||
await this.press(char, { delay: 20 }); // 键盘输入 | ||
} else { | ||
await this.sendCharacter(char); | ||
chars.push(char) | ||
} | ||
if (delay) { | ||
await new Promise(f => setTimeout(f, delay)); | ||
} | ||
await sleep(200) | ||
} | ||
// 直接赋值 | ||
if (chars.length) { | ||
await this.sendCharacter(chars.join('')); | ||
} | ||
} | ||
@@ -158,0 +166,0 @@ |
@@ -31,6 +31,10 @@ "use strict" | ||
let { isTouch, geolocation = {}, viewport } = options | ||
let { viewport, isTouch, geolocation } = options | ||
if (viewport) { | ||
await this.send('Emulation.setDeviceMetricsOverride', viewport) | ||
await this.send('Emulation.setDeviceMetricsOverride', { | ||
deviceScaleFactor: 1, | ||
...viewport | ||
}) | ||
} | ||
@@ -170,10 +174,2 @@ | ||
/** | ||
* 获取网页标题 | ||
*/ | ||
async title() { | ||
return await this.evaluate(document => document.title) | ||
} | ||
/** | ||
* 点击元素 | ||
@@ -184,14 +180,8 @@ * @param {String} selector CSS选择器 | ||
let bounding = await this.getBoundingRect(selector) | ||
let element = await this.$(selector) | ||
if (bounding) { | ||
if (element) { | ||
// 定位到元素中心 | ||
let { x, y, width, height } = bounding | ||
x = x + width / 2 | ||
y = y + height / 2 | ||
await this.clicker.click(x, y) | ||
await element.click() | ||
return bounding | ||
} | ||
@@ -269,4 +259,20 @@ | ||
} | ||
/** | ||
* 获取网页标题 | ||
*/ | ||
async title() { | ||
return await this.evaluate(document => document.title) | ||
} | ||
/** | ||
* 获取url | ||
*/ | ||
async url() { | ||
return await this.evaluate(() => window.location.href) | ||
} | ||
} | ||
module.exports = Page |
{ | ||
"name": "auto-chrome", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"description": "使用Node.js操作Chrome或Chromium,高仿真的用户行为模拟器", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,3 +10,3 @@ "use strict" | ||
let { userAgent, viewport } = devices['iPhone 6']; | ||
let { userAgent, viewport, isTouch } = devices['iPhone 6']; | ||
@@ -28,3 +28,4 @@ async function run() { | ||
}, | ||
viewport | ||
viewport, | ||
isTouch | ||
}, | ||
@@ -50,4 +51,6 @@ // devtools: true, | ||
let elment = await chrome.page.$('.c-result:nth-child(3)') | ||
let elments = await chrome.page.$$('#results .c-result') | ||
let elment = elments[6] | ||
await elment.scroll() | ||
@@ -59,2 +62,10 @@ | ||
// await elment.evaluate({ | ||
// func: (element) => { | ||
// element.style.border = "1px solid #ed0000" | ||
// }, | ||
// }) | ||
return | ||
await sleep(1500) | ||
@@ -83,3 +94,3 @@ | ||
await await chrome.page.goBack() | ||
await sleep(2000) | ||
@@ -86,0 +97,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
566215
56
2691