Greasy Fork

强制缩放与桌面模式

浏览器ua为手机ua时启用强制缩放,浏览器ua非手机ua时启用桌面模式

目前为 2023-06-04 提交的版本。查看 最新版本

// ==UserScript==
// @name        强制缩放与桌面模式
// @author      Lemon399
// @description 浏览器ua为手机ua时启用强制缩放,浏览器ua非手机ua时启用桌面模式
// @match       *://*/*
// @grant       none
// @version     6.3
// @run-at      document-start
// @namespace https://greasyfork.org/users/452911
// ==/UserScript==

function zoom() {
(function() {
  // 强制缩放比例
  const FORCE_SCALE = 1.0;
  // 默认视口宽度
  const DEFAULT_WIDTH = 1080;

  // 获取meta标签
  const metaTag = document.querySelector('meta[name=viewport]');
  // 获取浏览器UA字符
  const userAgent = navigator.userAgent;

  // 修改缩放比例
  function changeScale(scale) {
    metaTag.setAttribute('content', `width=device-width,initial-scale=${scale},maximum-scale=10.0,user-scalable=1`);
  };

  // 自动根据屏幕尺寸调整缩放比例
  function autoChangeScale() {
    // 如果不是移动浏览器,则将视口宽度设为默认值
    if (userAgent.indexOf('Mobile') < 0 || window.innerWidth !== screen.width) {
      metaTag.setAttribute('content', `width=${DEFAULT_WIDTH}`);
    }
    // 如果是移动浏览器且视口宽度等于屏幕宽度,则强制缩放
    else if (window.innerWidth === screen.width) {
      changeScale(FORCE_SCALE);
    }
  }

  // 初次加载页面时自动调整缩放比例
  autoChangeScale();
})();
}
zoom();
window.onload = function() {
  window.setTimeout(zoom,300);
};