xiaohongshu
xiaohongshu
chrome-automation
Chrome 浏览器自动化操作。当用户需要自动化浏览器操作、网页测试、数据抓取或 UI 自动化时使用此技能。
Full skill instructions
此技能专门用于 Chrome 浏览器自动化,包括:
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch({
headless: false, // 显示浏览器
slowMo: 50 // 减慢操作速度
});
const page = await browser.newPage();
// 导航到页面
await page.goto('https://example.com', {
waitUntil: 'networkidle2' // 等待网络空闲
});
// 前进后退
await page.goBack();
await page.goForward();
await page.reload();
await browser.close();
// 点击元素
await page.click('#submit-button');
// 输入文本
await page.type('#username', '[email protected]');
await page.type('#password', 'password123');
// 选择下拉框
await page.select('#country', 'CN');
// 上传文件
const fileInput = await page.$('input[type="file"]');
await fileInput.uploadFile('/path/to/file.pdf');
// 等待元素
await page.waitForSelector('.result', { timeout: 5000 });
// 获取元素文本
const text = await page.$eval('.title', el => el.textContent);
// 获取多个元素
const items = await page.$$eval('.item', elements =>
elements.map(el => el.textContent)
);
async function fillForm(page: Page) {
// 填写文本输入框
await page.type('#name', '张三');
await page.type('#email', '[email protected]');
await page.type('#phone', '13800138000');
// 选择单选按钮
await page.click('input[name="gender"][value="male"]');
// 选择复选框
await page.click('#agree-terms');
await page.click('#subscribe-newsletter');
// 选择下拉框
await page.select('#city', 'beijing');
// 填写日期
await page.type('#birthday', '1990-01-01');
// 填写文本域
await page.type('#message', '这是一条测试消息');
// 提交表单
await page.click('button[type="submit"]');
// 等待提交完成
await page.waitForNavigation();
}
async function scrapeData(url: string) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
// 等待内容加载
await page.waitForSelector('.product-list');
// 抓取数据
const products = await page.$$eval('.product-item', items =>
items.map(item => ({
title: item.querySelector('.title')?.textContent,
price: item.querySelector('.price')?.textContent,
image: item.querySelector('img')?.src,
link: item.querySelector('a')?.href
}))
);
await browser.close();
return products;
}
// 全页截图
await page.screenshot({
path: 'screenshot.png',
fullPage: true
});
// 元素截图
const element = await page.$('.chart');
await element.screenshot({ path: 'chart.png' });
// 生成 PDF
await page.pdf({
path: 'page.pdf',
format: 'A4',
printBackground: true,
margin: {
top: '20px',
right: '20px',
bottom: '20px',
left: '20px'
}
});
async function measurePerformance(url: string) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// 开始性能追踪
await page.tracing.start({ path: 'trace.json' });
await page.goto(url, { waitUntil: 'networkidle2' });
// 停止追踪
await page.tracing.stop();
// 获取性能指标
const metrics = await page.metrics();
console.log('性能指标:', metrics);
// 获取 Performance API 数据
const performanceData = await page.evaluate(() => {
const timing = performance.timing;
return {
loadTime: timing.loadEventEnd - timing.navigationStart,
domReady: timing.domContentLoadedEventEnd - timing.navigationStart,
firstPaint: performance.getEntriesByType('paint')[0]?.startTime
};
});
await browser.close();
return performanceData;
}
await page.setRequestInterception(true);
page.on('request', request => {
// 阻止图片加载
if (request.resourceType() === 'image') {
request.abort();
}
// 修改请求头
else if (request.url().includes('api')) {
request.continue({
headers: {
...request.headers(),
'Authorization': 'Bearer token'
}
});
}
else {
request.continue();
}
});
const iPhone = puppeteer.devices['iPhone 12'];
await page.emulate(iPhone);
await page.goto('https://example.com');
// 处理 alert/confirm/prompt
page.on('dialog', async dialog => {
console.log(dialog.message());
await dialog.accept(); // 或 dialog.dismiss()
});
// 处理新窗口
const newPagePromise = new Promise(resolve =>
browser.once('targetcreated', target => resolve(target.page()))
);
await page.click('a[target="_blank"]');
const newPage = await newPagePromise;
// 设置 Cookie
await page.setCookie({
name: 'session',
value: 'abc123',
domain: 'example.com'
});
// 获取 Cookie
const cookies = await page.cookies();
// 删除 Cookie
await page.deleteCookie({ name: 'session' });
// 在页面上下文中执行代码
const result = await page.evaluate(() => {
return document.title;
});
// 传递参数
const sum = await page.evaluate((a, b) => {
return a + b;
}, 5, 3);
// 暴露函数给页面
await page.exposeFunction('md5', (text: string) => {
return crypto.createHash('md5').update(text).digest('hex');
});
import { test, expect } from '@playwright/test';
test.describe('登录功能', () => {
test('成功登录', async ({ page }) => {
await page.goto('https://example.com/login');
await page.fill('#email', '[email protected]');
await page.fill('#password', 'password123');
await page.click('button[type="submit"]');
await expect(page).toHaveURL('https://example.com/dashboard');
await expect(page.locator('h1')).toContainText('欢迎');
});
test('登录失败提示', async ({ page }) => {
await page.goto('https://example.com/login');
await page.fill('#email', '[email protected]');
await page.fill('#password', 'wrongpass');
await page.click('button[type="submit"]');
await expect(page.locator('.error')).toBeVisible();
await expect(page.locator('.error')).toContainText('用户名或密码错误');
});
});
describe('购物车功能', () => {
beforeEach(() => {
cy.visit('/products');
});
it('添加商品到购物车', () => {
cy.get('.product-item').first().within(() => {
cy.get('.add-to-cart').click();
});
cy.get('.cart-badge').should('contain', '1');
});
it('从购物车删除商品', () => {
cy.get('.cart-icon').click();
cy.get('.cart-item').first().within(() => {
cy.get('.remove-button').click();
});
cy.get('.cart-empty').should('be.visible');
});
});
import puppeteer from 'puppeteer-extra';
import StealthPlugin from 'puppeteer-extra-plugin-stealth';
puppeteer.use(StealthPlugin());
const browser = await puppeteer.launch({
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-blink-features=AutomationControlled'
]
});
const page = await browser.newPage();
// 设置真实的 User-Agent
await page.setUserAgent(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
);
// 设置视口大小
await page.setViewport({
width: 1920,
height: 1080
});
// 隐藏 webdriver 标识
await page.evaluateOnNewDocument(() => {
Object.defineProperty(navigator, 'webdriver', {
get: () => false
});
});
async function robustScrape(url: string) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
try {
await page.goto(url, {
waitUntil: 'networkidle2',
timeout: 30000
});
const data = await page.evaluate(() => {
// 数据提取逻辑
});
return data;
} catch (error) {
console.error('抓取失败:', error);
// 截图保存错误现场
await page.screenshot({ path: 'error.png' });
throw error;
} finally {
await browser.close();
}
}
import pLimit from 'p-limit';
async function scrapeMultiplePages(urls: string[]) {
const browser = await puppeteer.launch();
const limit = pLimit(5); // 最多 5 个并发
const results = await Promise.all(
urls.map(url =>
limit(async () => {
const page = await browser.newPage();
try {
await page.goto(url);
return await page.evaluate(() => {
// 提取数据
});
} finally {
await page.close();
}
})
)
);
await browser.close();
return results;
}
// 禁用不必要的资源
await page.setRequestInterception(true);
page.on('request', request => {
const resourceType = request.resourceType();
if (['image', 'stylesheet', 'font'].includes(resourceType)) {
request.abort();
} else {
request.continue();
}
});
// 设置超时
page.setDefaultTimeout(10000);
page.setDefaultNavigationTimeout(30000);
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience