// ==UserScript== // @name 禁用Safari输入框缩放 // @namespace https://xyuxf.com/ // @version 0.2 // @description 在点击文本框时临时禁止缩放。 // @author 随缘先锋 // @match *://*/* // @grant none // @downloadURL https://update.greasyfork.icu/scripts/465021/%E7%A6%81%E7%94%A8Safari%E8%BE%93%E5%85%A5%E6%A1%86%E7%BC%A9%E6%94%BE.user.js // @updateURL https://update.greasyfork.icu/scripts/465021/%E7%A6%81%E7%94%A8Safari%E8%BE%93%E5%85%A5%E6%A1%86%E7%BC%A9%E6%94%BE.meta.js // ==/UserScript== (function() { 'use strict'; // 兼容 iPadOS 桌面模式 const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1); if (!isIOS) return; let originalContent = ''; const metaViewport = document.querySelector('meta[name=viewport]') || document.createElement('meta'); // 如果没有 meta 标签,创建一个并插入 if (!metaViewport.parentElement) { metaViewport.name = 'viewport'; metaViewport.content = 'width=device-width, initial-scale=1'; // 默认值 document.head.appendChild(metaViewport); } // 临时添加 user-scalable=no const lockZoom = () => { originalContent = metaViewport.getAttribute('content'); // 只有当当前允许缩放时,才进行锁定,避免重复操作 if (!/user-scalable=no/.test(originalContent)) { // 强制禁止缩放 metaViewport.setAttribute('content', `${originalContent}, user-scalable=no`); } }; //恢复原有状态 const unlockZoom = () => { if (originalContent) { metaViewport.setAttribute('content', originalContent); } }; // 监听所有输入框的聚焦和失焦 document.addEventListener('focus', (event) => { const target = event.target; // 只有是文本输入类的元素才触发 if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.isContentEditable) { // 排除 range, checkbox 等不需要键盘的 input if (!['checkbox', 'radio', 'range', 'button', 'submit'].includes(target.type)) { lockZoom(); } } }, true); document.addEventListener('blur', () => { unlockZoom(); }, true); })();