当前位置: 首页 > news >正文

利用GPT实现油猴脚本—网页滚动(优化版)

在浏览网页的时候,发现有的网页没有直达最前这样的功能,所有心血来潮利用ChatGPT写了一个油猴脚本以实现此功能,在网站上出现一个可以自由拖动的滑块。
声明:引用或二创需注明出处。


如图:
点击即可直达当前网页最前、中间、最后。
可以任意在网页上推动,点击小齿轮有简单的设置。
感觉可以在优化一下,直接发布脚本了。
滑块
在这里插入图片描述


脚本在此,可以复制自己用哟:

// ==UserScript==
// @name         滚动助手(优化版)
// @namespace    https://github.com/yourname/scroll-helper
// @version      1.0
// @description  支持快捷滚动、自定义热键、图标拖动、暗色模式、图形化设置中心,适配所有网页
// @author       GPT、依旧天真无邪
// @match        *://*/*
// @require      https://unpkg.com/sweetalert2@10.16.6/dist/sweetalert2.all.min.js
// @grant        none
// ==/UserScript==(function () {'use strict';const defaultConfig = {scrollKeys: { top: 'ArrowUp', bottom: 'ArrowDown' },autoShow: true,iconSize: 18,iconGap: 8,borderRadius: 12,panelOpacity: 60,iconPos: { right: 30, bottom: 100 },darkMode: false,keyEnabled: true};const storageKey = 'ScrollHelperConfig';const config = Object.assign({}, defaultConfig, JSON.parse(localStorage.getItem(storageKey) || '{}'));const isDark = config.darkMode || window.matchMedia('(prefers-color-scheme: dark)').matches;const Util = {saveConfig() {localStorage.setItem(storageKey, JSON.stringify(config));},createBtn(id, label, title, onClick) {const btn = document.createElement('button');btn.id = id;btn.textContent = label;btn.title = title;Object.assign(btn.style, {all: 'unset',fontSize: `${config.iconSize}px`,padding: '6px',borderRadius: `${config.borderRadius}px`,background: isDark ? '#444' : '#007BFF',color: '#fff',cursor: 'pointer',boxShadow: '0 1px 4px rgba(0,0,0,0.2)',textAlign: 'center',transition: 'transform 0.2s'});btn.onmouseover = () => btn.style.transform = 'scale(1.1)';btn.onmouseout = () => btn.style.transform = 'scale(1.0)';btn.onmousedown = () => btn.style.transform = 'scale(0.9)';btn.onmouseup = () => btn.style.transform = 'scale(1.1)';btn.onclick = onClick;return btn;},limitToViewport(pos) {const w = window.innerWidth;const h = window.innerHeight;pos.right = Math.min(Math.max(0, pos.right), w - 50);pos.bottom = Math.min(Math.max(0, pos.bottom), h - 50);return pos;}};const UI = {panel: null,createPanel() {const panel = document.createElement('div');panel.id = 'scroll-helper-panel';Object.assign(panel.style, {position: 'fixed',right: `${config.iconPos.right}px`,bottom: `${config.iconPos.bottom}px`,display: 'flex',flexDirection: 'column',gap: `${config.iconGap}px`,zIndex: 99999,opacity: config.panelOpacity / 100,cursor: 'move'});this.panel = panel;const buttons = [Util.createBtn('scroll-top', '⬆️', '返回顶部', () => window.scrollTo({ top: 0, behavior: 'smooth' })),Util.createBtn('scroll-mid', '↕️', '滚动中部', () => window.scrollTo({ top: (document.body.scrollHeight - window.innerHeight) / 2, behavior: 'smooth' })),Util.createBtn('scroll-bottom', '⬇️', '滚动到底', () => window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' })),Util.createBtn('scroll-settings', '⚙️', '设置中心', UI.openSettings)];buttons.forEach(btn => panel.appendChild(btn));document.body.appendChild(panel);this.enableDrag();},enableDrag() {let isDragging = false, startX, startY;this.panel.addEventListener('mousedown', (e) => {isDragging = true;startX = e.clientX;startY = e.clientY;e.preventDefault();});window.addEventListener('mousemove', (e) => {if (!isDragging) return;const dx = e.clientX - startX;const dy = e.clientY - startY;config.iconPos.right -= dx;config.iconPos.bottom -= dy;Util.limitToViewport(config.iconPos);this.panel.style.right = `${config.iconPos.right}px`;this.panel.style.bottom = `${config.iconPos.bottom}px`;startX = e.clientX;startY = e.clientY;});window.addEventListener('mouseup', () => {if (isDragging) {isDragging = false;Util.saveConfig();}});},openSettings() {const html = `<div style="text-align:left"><label>图标大小:<input type="number" id="set-iconSize" value="${config.iconSize}" style="width:60px"></label><br><br><label>圆角半径:<input type="number" id="set-radius" value="${config.borderRadius}" style="width:60px"></label><br><br><label>透明度(%):<input type="number" id="set-opacity" value="${config.panelOpacity}" style="width:60px"></label><br><br><label><input type="checkbox" id="set-autoShow" ${config.autoShow ? 'checked' : ''}> 自动显示滚动按钮</label><br><br><label><input type="checkbox" id="set-keyEnabled" ${config.keyEnabled ? 'checked' : ''}> 启用快捷键 Ctrl+↑↓</label><br><br><label><input type="checkbox" id="set-darkMode" ${config.darkMode ? 'checked' : ''}> 暗色模式</label></div>`;Swal.fire({title: '滚动助手设置',html,confirmButtonText: '保存设置',showCancelButton: true,preConfirm: () => {config.iconSize = parseInt(document.getElementById('set-iconSize').value);config.borderRadius = parseInt(document.getElementById('set-radius').value);config.panelOpacity = parseInt(document.getElementById('set-opacity').value);config.autoShow = document.getElementById('set-autoShow').checked;config.keyEnabled = document.getElementById('set-keyEnabled').checked;config.darkMode = document.getElementById('set-darkMode').checked;Util.saveConfig();}}).then(res => {if (res.isConfirmed) {location.reload(); // 刷新页面以应用新设置}});},handleScrollDisplay() {if (!config.autoShow) {this.panel.style.display = 'flex';return;}window.addEventListener('scroll', () => {if (window.scrollY > 200) {this.panel.style.display = 'flex';} else {this.panel.style.display = 'none';}});}};const Shortcut = {init() {if (!config.keyEnabled) return;document.addEventListener('keydown', (e) => {if (e.ctrlKey && e.key === config.scrollKeys.top) {window.scrollTo({ top: 0, behavior: 'smooth' });}if (e.ctrlKey && e.key === config.scrollKeys.bottom) {window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });}});}};function init() {UI.createPanel();UI.handleScrollDisplay();Shortcut.init();}if (document.readyState === 'complete' || document.body) {init();} else {window.addEventListener('DOMContentLoaded', init);}})();
http://www.xdnf.cn/news/353665.html

相关文章:

  • 豆包:基于多模态交互的智能心理咨询机器人系统设计与效果评估——情感计算框架下的对话机制创新
  • Spark,在shell中运行RDD程序
  • 【SQL系列】多表关联更新
  • 手持气象仪:能够实时测量多种气象参数,保数据采集的准确性与实时性
  • 掌握Multi-Agent实践(三):ReAct Agent集成Bing和Google搜索功能,采用推理与执行交替策略,增强处理复杂任务能力
  • Spring Boot 框架概述
  • 【计算机视觉】Car-Plate-Detection-OpenCV-TesseractOCR:车牌检测与识别
  • 【css】css统一设置变量
  • 更新 / 安装 Nvidia Driver 驱动 - Ubuntu - 2
  • 数据类型详解(布尔值、整型、浮点型、字符串等)-《Go语言实战指南》
  • istio in action之Gateway流量入口与安全
  • 分析NVIDIA的股价和业绩暴涨的原因
  • Zabbix监控 RabbitMQ 指定消息队列名称(pull_alarms )的消费者
  • 富乐德传感技术盘古信息 | 锚定“未来工厂”新坐标,开启传感器制造行业数字化转型新征程
  • IC解析之TPS92682-Q1(汽车LED灯控制IC)
  • 【C/C++】C语⾔内存函数
  • [Errno 122] Disk quota exceeded
  • Linux59 SSH配置前瞻 JumpServer双网卡ping通
  • 金仓数据库永久增量备份技术原理与操作
  • 电商平台如何做好DDoS 攻防战?
  • 物流基础知识-术语 | 医药物流(1)
  • OpenHarmony平台驱动开发(十),MMC
  • k8s监控方案实践(二):集成Alertmanager告警与钉钉Webhook通知
  • C23 与 MISRA C:2025:嵌入式 C 语言的进化之路
  • 4.3【LLaMA-Factory实战】教育大模型:个性化学习路径生成系统全解析
  • 微服务中 本地启动 springboot 无法找到nacos配置 启动报错
  • 第十六章,网络型攻击防范技术
  • Python 常用内置函数详解(十):help()函数——查看对象的帮助信息
  • 【论文阅读27】-TCN–BiLSTM -滑坡预测
  • 从Dockerfile 构建docker镜像——保姆级教程