模拟竖屏

由于有些链接用小窗模式的浏览器打开,会提示切换到竖屏,特此模拟竖屏

目前為 2023-11-18 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         模拟竖屏
// @namespace    https://greasyfork.org/zh-CN/scripts/479590
// @supportURL   https://github.com/WhiteSevs/TamperMonkeyScript/issues
// @icon         https://favicon.yandex.net/favicon/v2/https://party.163.com/?size=32
// @version      2023.11.12
// @description  由于有些链接用小窗模式的浏览器打开,会提示切换到竖屏,特此模拟竖屏
// @author       WhiteSevs
// @license      GPL-3.0-only
// @match        https://eggy.163.com/*
// @match        https://igame.qq.com/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function () {
  /**
   * 油猴环境下就是unsafeWindow,如果不是就是globalThis
   * @type {window}
   */
  const win = typeof unsafeWindow === "object" ? unsafeWindow : globalThis;

  /**
   * 添加CSS到页面中
   * @param {string} cssText CSS字符串
   */
  const GM_addStyle = function (cssText) {
    if (cssText == null) {
      return;
    }
    if (typeof cssText !== "string") {
      return;
    }
    if (cssText.trim() === "") {
      return;
    }
    let cssNode = document.createElement("style");
    cssNode.setAttribute("type", "text/css");
    cssNode.innerHTML = cssText;
    if (document.head) {
      /* 插入head后 */
      document.head.appendChild(cssNode);
    } else if (document.documentElement.childNodes.length === 0) {
      /* 插入body后 */
      document.documentElement.appendChild(cssNode);
    } else {
      /* 插入head前面 */
      document.documentElement.insertBefore(
        cssNode,
        document.documentElement.childNodes[0]
      );
    }
  };

  /**
   * 配置
   * @typedef {object} verticalScreenWebSiteDetails
   * @property {string|undefined} hostName 匹配的网站hostName,和href任选其一
   * @property {string|undefined} href 匹配的网站href,和hostName任选其一
   * @property {boolean} hijackOrientation 是否处理window.orientation
   * @property {string|undefined} CSS 竖屏CSS
   */
  /**
   * @type {verticalScreenWebSiteDetails[]}
   */
  const verticalScreenWebSite = [
    {
      hostName: "eggy.163.com",
      hijackOrientation: true,
    },
    {
      hostName: "igame.qq.com",
      hijackOrientation: true,
      CSS: `@charset "UTF-8";@media all and (orientation:landscape){html{width:unset;height:unset;overflow:unset}
            html::before{position:unset;top:unset;left:unset;z-index:unset;width:unset;height:unset;background:unset;content:unset}
            body::after{content:unset;text-align:unset;font-size:unset;color:unset;position:unset;top:unset;left:unset;height:unset;width:unset;margin-top:unset;z-index:unset}
            body::before{content:unset;position:unset;z-index:unset;height:unset;width:unset;left:unset;top:unset;margin:unset;color:unset;background-image:unset;background-repeat:unset;background-position:unset;background-size:unset;-webkit-transform:unset}}`,
    },
  ];

  /**
   * 劫持
   */
  const hijack = {
    /**
     * 劫持window.orientation
     */
    hijackOrientation() {
      Object.defineProperty(win, "orientation", {
        /**
         * 0是正常竖屏
         * 90是倒着的竖屏
         * @returns {number}
         */
        get() {
          console.log("模拟竖屏");
          return 0;
        },
      });
    },
  };

  /**
   * 模拟
   */
  const simulation = function () {
    for (let index = 0; index < verticalScreenWebSite.length; index++) {
      let details = verticalScreenWebSite[index];
      if (
        (typeof details.hostName === "string" &&
          window.location.hostname === details.hostName.trim()) ||
        (typeof details.href === "string" &&
          window.location.href.match(details.href))
      ) {
        console.log(`添加在${window.location.hostname}下的模拟竖屏CSS`);
        GM_addStyle(details.CSS);
        if (details.hijackOrientation) {
          hijack.hijackOrientation();
        }
        break;
      }
    }
  };

  /* --------------------入口-------------------- */
  simulation();
  /* --------------------入口-------------------- */
})();