HLJ 发布于
2025-05-22 15:32:32
0阅读

20个实用JavaScript自动化脚本推荐

以下是 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订阅检测

场景:查找当前页面的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);
})();

十八、复制标题和URL

场景:快速复制当前页面标题和链接。

javascript:(function(){
  const text = `${document.title}\n${location.href}`;
  navigator.clipboard.writeText(text);
  alert('已复制到剪贴板!');
})();

十九、自定义CSS注入

场景:临时修改页面样式。

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'
  });
})();

使用方法

  1. 书签脚本:将代码保存为浏览器书签(前缀保留 javascript:)。
  2. 控制台运行:直接粘贴到浏览器开发者工具控制台。
  3. 注意事项:部分脚本需根据目标网站调整选择器或权限。

这些脚本可大幅减少重复性操作,根据实际需求灵活修改代码参数!

当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2025-05-22/699.html
最后生成于 2025-06-05 15:00:18
此内容有帮助 ?
0