核心限制
瀏覽器**同源策略**禁止網頁主動存取/操作其他域名的頁面。在開發者工具 Console 手寫的腳本,頁面刷新/跳轉後即失效(上下文銷毀)。
三種可行方案
方案一:瀏覽器擴展 - 最標準、權限可控
// manifest.json (Manifest V3)
{
"name": "Auto Register Bot",
"version": "1.0",
"manifest_version": 3,
"permissions": ["scripting", "activeTab"],
"host_permissions": ["https://target-site.com/*"],
"background": {
"service_worker": "background.js"
},
"content_scripts": [{
"matches": ["https://target-site.com/register*"],
"js": ["content.js"],
"run_at": "document_idle"
}]
}
// content.js - 自動在註冊頁執行
(function() {
'use strict';
function autoRegister() {
// 填寫表單
document.querySelector('#username').value = genUser();
document.querySelector('#email').value = genEmail();
document.querySelector('#password').value = 'fixedPass123';
// 提交 - 使用 fetch 避免頁面跳轉
fetch('/api/register', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({...})
}).then(r => r.json()).then(data => {
if (data.success) {
console.log('註冊成功', data);
setTimeout(autoRegister, 2000); // 下一輪
}
});
}
if (location.pathname.includes('/register')) {
autoRegister();
}
})();
方案二:Tampermonkey / Violentmonkey 用戶腳本 - 快速原型
// ==UserScript==
// @name Auto Register
// @match https://target-site.com/register*
// @grant GM_xmlhttpRequest
// @run-at document-end
// ==/UserScript==
(async function() {
while (true) {
// 填寫表單...
// 使用 GM_xmlhttpRequest 繞過 CSP 發送請求
await GM_xmlhttpRequest({
method: 'POST',
url: '/api/register',
data: JSON.stringify(payload),
headers: {'Content-Type': 'application/json'},
onload: (r) => { console.log(r.responseText); }
});
await sleep(2000);
}
})();
方案三:Puppeteer / Playwright 自動化 - 伺服器端批量
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
for (let i = 0; i < 100; i++) {
await page.goto('https://target-site.com/register');
await page.type('#username', `user${i}`);
await page.type('#email', `user${i}@example.com`);
await page.type('#password', 'pass123');
// 監聽網路請求而非點擊按鈕
const [response] = await Promise.all([
page.waitForResponse(r => r.url().includes('/api/register')),
page.click('#submit-btn')
]);
console.log(await response.json());
await page.waitForTimeout(1000);
}
await browser.close();
})();
關鍵技巧:避免表單提交導致頁面跳轉
| 錯誤做法 | 正確做法 |
form.submit() / 點擊 submit 按鈕 | 用 fetch / XMLHttpRequest / GM_xmlhttpRequest 直接呼叫 API |
| 等待頁面跳轉 | 監聽 API 回應,停留在原頁繼續下一輪 |
法律與道德警示
- ⚠️ **批量註冊多數違反網站服務條款 (ToS)**,可能構成計算機欺詐濫用法案 (CFAA) 違規
- ⚠️ 驗證碼、設備指紋、IP 限流等反自動化機制會攔截
- ✅ 僅用於**授權測試環境**、自家業務自動化、學術研究
方案選型建議