// ==UserScript==
// @name         New Userscript
// @namespace    https://viayoo.com/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @run-at       document-end
// @match        https://*/*
// @grant        none
// ==/UserScript==

// ==UserScript== // @name 移动端视频下载助手 // @namespace http://tampermonkey.net/ // @version 2.0 // @description 在视频右上角添加下载按钮，优化移动端体验 // @author 你的名字 // @match *://*/* // @grant none // @run-at document-idle // ==/UserScript== (function() { 'use strict'; // 创建下载按钮样式（响应式设计） const style = document.createElement('style'); style.innerHTML = ` .video-download-btn { position: absolute; top: 10px; right: 10px; z-index: 9999; background: linear-gradient(45deg, #ff416c, #ff4b2b); color: white; border: none; padding: 8px 16px; border-radius: 50px; font-weight: bold; cursor: pointer; opacity: 0.85; transition: all 0.3s ease; box-shadow: 0 2px 10px rgba(0,0,0,0.3); font-size: 14px; display: flex; align-items: center; gap: 6px; } .video-download-btn:hover { opacity: 1; transform: translateY(-2px); box-shadow: 0 4px 15px rgba(0,0,0,0.4); } /* 移动端样式 */ @media (max-width: 768px) { .video-download-btn { padding: 6px 12px; font-size: 12px; top: 5px; right: 5px; } } /* 触摸设备优化 */ @media (hover: none) and (pointer: coarse) { .video-download-btn { padding: 10px 18px; font-size: 14px; opacity: 0.95; } } `; document.head.appendChild(style); // 创建下载按钮 function createDownloadButton(video) { // 检查是否已存在按钮 if (video.parentNode.querySelector('.video-download-btn')) return; const btn = document.createElement('button'); btn.className = 'video-download-btn'; btn.innerHTML = '⬇️ 下载'; // 添加触摸事件支持 btn.addEventListener('click', function(e) { e.stopPropagation(); downloadVideo(video); }); // 添加到视频容器 const container = getVideoContainer(video); container.style.position = 'relative'; container.appendChild(btn); } // 获取合适的视频容器（移动端适配） function getVideoContainer(video) { // 在移动端，视频通常被包裹在特定容器中 let container = video; for (let i = 0; i < 3; i++) { container = container.parentElement; if (container.clientWidth > video.offsetWidth * 0.9) { return container; } } return video.parentElement; } // 下载视频功能 function downloadVideo(video) { const videoUrl = video.currentSrc || video.src; if (!videoUrl) { alert('无法获取视频地址'); return; } const fileName = getFileNameFromUrl(videoUrl); const a = document.createElement('a'); a.href = videoUrl; a.download = fileName; document.body.appendChild(a); a.click(); document.body.removeChild(a); } // 从URL中提取文件名 function getFileNameFromUrl(url) { try { const urlObj = new URL(url); const pathname = urlObj.pathname; const filename = pathname.substring(pathname.lastIndexOf('/') + 1); return filename.split('?')[0] || 'video.mp4'; } catch (e) { return 'video.mp4'; } } // 查找并处理视频元素（移动端优化） function findAndProcessVideos() { document.querySelectorAll('video').forEach(video => { // 移动端视频尺寸可能较小，调整阈值 const minSize = isMobile() ? 100 : 200; if (video.offsetWidth > minSize && video.offsetHeight > minSize/2) { createDownloadButton(video); } }); } // 检测是否为移动设备 function isMobile() { return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); } // 初始处理 findAndProcessVideos(); // 使用MutationObserver监听DOM变化（移动端动态加载） const observer = new MutationObserver(mutations => { mutations.forEach(mutation => { if (mutation.addedNodes.length) { // 增加延迟处理，确保移动端动态元素加载完成 setTimeout(findAndProcessVideos, 500); } }); }); observer.observe(document.body, { childList: true, subtree: true }); // 监听视窗变化（移动端旋转屏幕） window.addEventListener('resize', findAndProcessVideos); })();