改写自己的浏览器插件工具 myChromeTools
1. 起因, 目的:
前面我写过, 自己的一个浏览器插件小工具
- 最近又增加一个小功能,可以自动滚动页面,尤其是对于那些瀑布流加载的网页。
- 最新的代码都在这里
2. 先看效果
3. 过程:
代码 1, 模拟鼠标自然滚动
// 处理滚动控制逻辑,响应 popup.js 发送的 startScroll 和 stopScroll 消息。(function () {let scrollInterval = null;function smoothAutoScroll() {if (scrollInterval) {clearInterval(scrollInterval);}scrollInterval = setInterval(() => {const scrollHeight = document.documentElement.scrollHeight;const windowHeight = window.innerHeight;const currentScroll = window.scrollY;if (currentScroll + windowHeight >= scrollHeight - 1) {window.scrollTo({ top: 0, behavior: 'smooth' });} else {window.scrollBy({ top: 2, behavior: 'smooth' });}}, 16); // 每16毫秒滚动一次,约60fps}function stopScroll() {if (scrollInterval) {clearInterval(scrollInterval);scrollInterval = null;}}chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {if (message.action === 'startScroll') {smoothAutoScroll();} else if (message.action === 'stopScroll') {stopScroll();}});})();
4. 结论 + todo
- 可以试试看,默认人工滚动,即滚滚停停,假装是在阅读。
希望对大家有帮助。