强制缩放与桌面模式

浏览器ua为手机ua时启用强制缩放,浏览器ua非手机ua时启用桌面模式,脚本菜单可以单独设置桌面模式宽度或全局宽度

  1. // ==UserScript==
  2. // @name 强制缩放与桌面模式
  3. // @author Lemon399
  4. // @description 浏览器ua为手机ua时启用强制缩放,浏览器ua非手机ua时启用桌面模式,脚本菜单可以单独设置桌面模式宽度或全局宽度
  5. // @match *://*/*
  6. // @exclude https://sj.qq.com/*
  7. // @grant GM_registerMenuCommand
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // @grant GM_unsetValue
  11. // @version 9.2
  12. // @run-at document-start
  13. // @namespace https://greasyfork.org/users/452911
  14. // ==/UserScript==
  15.  
  16. const domain = window.location.hostname;
  17. let globalViewportWidth = GM_getValue('globalViewportWidth', 1080);
  18. let viewportWidth = GM_getValue(`viewportWidth_${domain}`, globalViewportWidth);
  19. let userAgent = navigator.userAgent;
  20.  
  21. // 检查脚本是否启用
  22. let scriptEnabled = GM_getValue('scriptEnabled', true);
  23.  
  24. function setViewportWidth(width) {
  25. viewportWidth = width;
  26. GM_setValue(`viewportWidth_${domain}`, width); // 将视口宽度与域名关联存储
  27. autoChangeScale(); // 更新视口宽度后自动调整缩放比例
  28. }
  29.  
  30. function setGlobalViewportWidth(width) {
  31. globalViewportWidth = width;
  32. GM_setValue('globalViewportWidth', width); // 设置全局视口宽度
  33. if (!GM_getValue(`viewportWidth_${domain}`)) {
  34. viewportWidth = width; // 更新当前网站的视口宽度,如果未单独设置过
  35. }
  36. autoChangeScale();
  37. }
  38.  
  39. function autoChangeScale() {
  40. if (!scriptEnabled) return; // 如果脚本禁用,则不执行缩放调整
  41.  
  42. const metaTag = document.querySelector('meta[name=viewport]');
  43. if (metaTag) {
  44. const isMobile =
  45. userAgent.indexOf('Mobile') < 0 &&
  46. userAgent.indexOf('SymbianOS') < 0 &&
  47. userAgent.indexOf('SearchCraft') < 0;
  48.  
  49. metaTag.setAttribute('content', isMobile ? `width=${viewportWidth}` : 'width=device-width,initial-scale=1.0,maximum-scale=10.0,user-scalable=1');
  50. }
  51. }
  52.  
  53. autoChangeScale();
  54.  
  55. //监听url变化执行
  56. history.pushState = ( f => function pushState(){
  57. var ret = f.apply(this, arguments);
  58. window.dispatchEvent(new Event('pushstate'));
  59. window.dispatchEvent(new Event('urlchange'));
  60. return ret;
  61. })(history.pushState);
  62. history.replaceState = ( f => function replaceState(){
  63. var ret = f.apply(this, arguments);
  64. window.dispatchEvent(new Event('replacestate'));
  65. window.dispatchEvent(new Event('urlchange'));
  66. return ret;
  67. })(history.replaceState);
  68. window.addEventListener('popstate',()=>{
  69. window.dispatchEvent(new Event('urlchange'))
  70. });
  71.  
  72. window.addEventListener('urlchange', function(event) {
  73. window.setTimeout(autoChangeScale, 100);
  74. });
  75.  
  76. //双指执行
  77. document.addEventListener('touchmove', function(e) {
  78. if (e.touches.length > 1) {
  79. autoChangeScale();
  80. }
  81. });
  82.  
  83. //监听宽度变化
  84. const mediaQuery = window.matchMedia(`(width: ${viewportWidth}px)`);
  85.  
  86. function handleViewportChange(event) {
  87. if (!event.matches) {
  88. autoChangeScale();
  89. }
  90. }
  91.  
  92. mediaQuery.addEventListener('change', handleViewportChange);
  93. handleViewportChange(mediaQuery);
  94.  
  95. // 添加菜单命令
  96. GM_registerMenuCommand('设置此网站视口宽度', function() {
  97. const inputWidth = prompt('请输入视口宽度:', viewportWidth);
  98. if (inputWidth) {
  99. const parsedWidth = parseInt(inputWidth, 10);
  100. if (!isNaN(parsedWidth)) {
  101. setViewportWidth(parsedWidth);
  102. } else {
  103. alert('输入的宽度无效,请输入一个有效的数字!');
  104. }
  105. }
  106. });
  107.  
  108. GM_registerMenuCommand('设置全局视口宽度', function() {
  109. const inputWidth = prompt('请输入全局视口宽度:', globalViewportWidth);
  110. if (inputWidth) {
  111. const parsedWidth = parseInt(inputWidth, 10);
  112. if (!isNaN(parsedWidth)) {
  113. setGlobalViewportWidth(parsedWidth);
  114. } else {
  115. alert('输入的宽度无效,请输入一个有效的数字!');
  116. }
  117. }
  118. });
  119.  
  120. // 启用/禁用脚本菜单命令
  121. GM_registerMenuCommand(scriptEnabled ? '在此网站禁用' : '在此网站启用', function() {
  122. scriptEnabled = !scriptEnabled;
  123. GM_setValue('scriptEnabled', scriptEnabled);
  124. if (scriptEnabled) {
  125. autoChangeScale(); // 启用脚本并立即应用设置
  126. GM_registerMenuCommand('在此网站禁用', function() {
  127. GM_setValue('scriptEnabled', false);
  128. location.reload(); // 禁用脚本并刷新页面
  129. });
  130. } else {
  131. GM_registerMenuCommand('在此网站启用', function() {
  132. GM_setValue('scriptEnabled', true);
  133. autoChangeScale(); // 启用脚本并立即应用设置
  134. });
  135. }
  136. });