Custom browser platform

Customize browser platform information so that you can freely access the mobile or desktop side of the target web site. This script is only valid for JavaScript detection and invalid for server-side detection.

目前為 2018-02-14 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name                        Custom browser platform
// @name:zh-CN                  自定义浏览器平台信息
// @namespace                   https://github.com/gam2046/userscript
// @description                 Customize browser platform information so that you can freely access the mobile or desktop side of the target web site. This script is only valid for JavaScript detection and invalid for server-side detection.
// @description:zh-CN           自定义浏览器平台信息,以便于你可以自由访问目标网站的移动端或者桌面端。此脚本仅针对JavaScript检测有效,对于服务端检测无效。
// @version                     2
// @match                       *://*/*
// @run-at                      document-start
// @grant                       GM_getValue
// @grant                       GM_setValue
// @grant                       GM_addStyle
// @grant                       GM_registerMenuCommand
// @supportURL                  https://github.com/gam2046/userscript/issues/new
// @require                     https://greasyfork.org/scripts/38445-monkeyconfig/code/MonkeyConfig.js
// @copyright                   2018+, forDream <gan2046#gmail.com>
// @author                      forDream
// ==/UserScript==

(function () {
    'use strict';

    function isMobileDevice() {
        try {
            document.createEvent("TouchEvent");
            return ('ontouchstart' in document.documentElement) &&
                (navigator.maxTouchPoints > 0 || 'ontouchstart' in document.documentElement) &&
                window.orientation > -1;
        }
        catch (e) {
            return false;
        }
    }

    var originalUA = navigator.userAgent;
    var originalPlatform = navigator.platform;

    var cfg = new MonkeyConfig({
        title: 'Custom Platform Information',
        menuCommand: true,
        params: {
            UserAgent: {
                type: 'text',
                default: originalUA
            },
            Platform: {
                type: 'text',
                default: originalPlatform
            },
            'Analog mobile device': {
                type: 'checkbox',
                default: false,
                enable: false
            },
            'Analog desktop device': {
                type: 'checkbox',
                default: false,
                enable: false
            }
        }
    });

    var customUA = cfg.get('UserAgent');
    var customPlatform = cfg.get('Platform');

    // 如果同时勾选移动端与桌面端模拟,则忽略桌面端选项
    if (true == cfg.get('Analog mobile device')) {
        cfg.set('Analog desktop device', false);
        window.orientation = 1;
        document.documentElement.ontouchstart =
            document.documentElement.ontouchmove =
            document.documentElement.ontouchend =
            document.documentElement.ontouchcancel = function () { };
        Object.defineProperty(navigator, 'maxTouchPoints', { get: function () { return 5; } });
    } else if (true == cfg.get('Analog desktop device')) {
        window.orientation = undefined;
        document.documentElement.ontouchstart =
            document.documentElement.ontouchmove =
            document.documentElement.ontouchend =
            document.documentElement.ontouchcancel = undefined;
        Object.defineProperty(navigator, 'maxTouchPoints', { get: function () { return 0; } });
    }

    Object.defineProperty(navigator, 'userAgent', { get: function () { return customUA; } });
    Object.defineProperty(navigator, 'platform', { get: function () { return customPlatform; } });

    console.log('Current is mobile ', isMobileDevice());
})();