以下是 20个实用的JavaScript自动化脚本,帮助你提升浏览器操作效率,节省重复性工作的时间。这些脚本可直接在浏览器控制台运行或保存为书签脚本(Bookmarklet),适用于常见场景:
场景:快速填写测试表单或重复性表单。
javascript:(function(){
document.querySelector('#username').value = 'your_username';
document.querySelector('#password').value = 'your_password';
document.querySelector('#email').value = 'test@example.com';
// 根据实际表单字段ID修改
})();
场景:一键切换页面为深色模式。
javascript:(function(){
document.body.style.filter = document.body.style.filter === 'invert(1)' ? 'none' : 'invert(1)';
})();
场景:自动滚动页面到底部(用于无限加载页面)。
// 滚动到底部
javascript:window.scrollTo(0, document.body.scrollHeight);
// 滚动到顶部
javascript:window.scrollTo(0, 0);
场景:快速标记所有外部链接。
javascript:(function(){
const links = document.querySelectorAll('a[href^="http"]');
links.forEach(link => link.style.backgroundColor = 'yellow');
})();
场景:自动点击下一页按钮(需适配目标网站)。
javascript:(function(){
const nextBtn = document.querySelector('.next-page'); // 修改为实际选择器
if (nextBtn) nextBtn.click();
})();
场景:隐藏常见广告元素。
javascript:(function(){
const ads = document.querySelectorAll('.ad-class, [id*="ad"]');
ads.forEach(ad => ad.style.display = 'none');
})();
场景:生成当前页面的Twitter分享链接。
javascript:(function(){
const url = encodeURIComponent(window.location.href);
const text = encodeURIComponent(document.title);
window.open(`https://twitter.com/intent/tweet?text=${text}&url=${url}`);
})();
场景:高亮页面中的特定关键词(如“重要”)。
javascript:(function(){
const keyword = '重要';
const regex = new RegExp(keyword, 'gi');
document.body.innerHTML = document.body.innerHTML.replace(regex, '<mark>$&</mark>');
})();
场景:快速填充并提交登录表单(谨慎使用)。
javascript:(function(){
document.querySelector('#login').value = 'your_username';
document.querySelector('#password').value = 'your_password';
document.querySelector('form').submit();
})();
场景:快速翻译页面内容为英文(依赖Google Translate)。
javascript:(function(){
window.location.href = 'https://translate.google.com/translate?js=n&sl=auto&tl=en&u=' + encodeURIComponent(location.href);
})();
场景:批量下载页面所有图片(需注意版权)。
javascript:(function(){
const images = document.querySelectorAll('img');
images.forEach(img => {
const link = document.createElement('a');
link.href = img.src;
link.download = '';
link.click();
});
})();
场景:快速生成随机密码并复制到剪贴板。
javascript:(function(){
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*';
const password = Array.from(crypto.getRandomValues(new Uint32Array(12)))
.map(n => chars[n % chars.length]).join('');
navigator.clipboard.writeText(password);
alert('密码已复制: ' + password);
})();
场景:移除烦人的浮动元素或弹窗。
javascript:(function(){
const element = document.querySelector('.annoying-popup'); // 修改选择器
if (element) element.remove();
})();
场景:查找当前页面的RSS订阅链接。
javascript:(function(){
const rss = document.querySelector('link[type="application/rss+xml"]');
alert(rss ? '发现RSS: ' + rss.href : '未找到RSS');
})();
场景:快速获取页面加载时间。
javascript:(function(){
const timing = performance.timing;
const loadTime = timing.loadEventEnd - timing.navigationStart;
alert('页面加载时间: ' + loadTime + 'ms');
})();
场景:一键进入全屏模式。
javascript:(function(){
document.documentElement.requestFullscreen();
})();
场景:定时刷新页面(例如监控抢购)。
javascript:(function(){
const seconds = 30; // 修改间隔时间
setTimeout(() => location.reload(), seconds * 1000);
})();
场景:快速复制当前页面标题和链接。
javascript:(function(){
const text = `${document.title}\n${location.href}`;
navigator.clipboard.writeText(text);
alert('已复制到剪贴板!');
})();
场景:临时修改页面样式。
javascript:(function(){
const css = 'body { font-size: 18px !important; }'; // 修改CSS
const style = document.createElement('style');
style.innerHTML = css;
document.head.appendChild(style);
})();
场景:显示各阶段加载耗时。
javascript:(function(){
const t = performance.timing;
console.log({
DNS查询: t.domainLookupEnd - t.domainLookupStart + 'ms',
TCP连接: t.connectEnd - t.connectStart + 'ms',
页面加载: t.loadEventEnd - t.navigationStart + 'ms'
});
})();
javascript:
)。这些脚本可大幅减少重复性操作,根据实际需求灵活修改代码参数!