// ==UserScript== // @name 强制缩放与桌面模式 // @author Lemon399 // @description 浏览器ua为手机ua时启用强制缩放,浏览器ua非手机ua时启用桌面模式,脚本菜单可以单独设置桌面模式宽度 // @match *://*/* // @exclude https://sj.qq.com/* // @grant GM_registerMenuCommand // @grant GM_setValue // @grant GM_getValue // @version 8.9 // @run-at document-start // @namespace https://greasyfork.org/users/452911 // @downloadURL none // ==/UserScript== const domain = window.location.hostname; let viewportWidth = GM_getValue(`viewportWidth_${domain}`, 1080); let userAgent = navigator.userAgent; function setViewportWidth(width) { viewportWidth = width; GM_setValue(`viewportWidth_${domain}`, width); // 将视口宽度与域名关联存储 autoChangeScale(); // 更新视口宽度后自动调整缩放比例 } function autoChangeScale() { const metaTag = document.querySelector('meta[name=viewport]'); if (metaTag) { const isMobile = userAgent.indexOf('Mobile') < 0 && userAgent.indexOf('SymbianOS') < 0 && userAgent.indexOf('SearchCraft') < 0; metaTag.setAttribute('content', isMobile ? `width=${viewportWidth}` : 'width=device-width,initial-scale=1.0,maximum-scale=10.0,user-scalable=1'); } } autoChangeScale(); //监听url变化执行 history.pushState = ( f => function pushState(){ var ret = f.apply(this, arguments); window.dispatchEvent(new Event('pushstate')); window.dispatchEvent(new Event('urlchange')); return ret; })(history.pushState); history.replaceState = ( f => function replaceState(){ var ret = f.apply(this, arguments); window.dispatchEvent(new Event('replacestate')); window.dispatchEvent(new Event('urlchange')); return ret; })(history.replaceState); window.addEventListener('popstate',()=>{ window.dispatchEvent(new Event('urlchange')) }); window.addEventListener('urlchange', function(event) { window.setTimeout(autoChangeScale, 100); }); //延时执行 window.setTimeout(autoChangeScale, 250); //双指执行 document.addEventListener('touchmove', function(e) { if (e.touches.length > 1) { autoChangeScale(); } }); //监听宽度变化 // 创建一个MediaQueryList对象来监听宽度不等于1080px的变化 const mediaQuery = window.matchMedia('(width: 1080px)'); // 处理MediaQueryList对象的变化 function handleViewportChange(event) { if (!event.matches) { // 当viewport宽度不等于1080px时 autoChangeScale(); } } // 监听viewport宽度变化 mediaQuery.addEventListener('change', handleViewportChange); // 初次检查 handleViewportChange(mediaQuery); // 添加菜单命令 GM_registerMenuCommand('设置视口宽度', function() { const inputWidth = prompt('请输入视口宽度:', viewportWidth); if (inputWidth) { const parsedWidth = parseInt(inputWidth, 10); if (!isNaN(parsedWidth)) { setViewportWidth(parsedWidth); } else { alert('输入的宽度无效,请输入一个有效的数字!'); } } });