新浪微博PC版手机版相互跳转

点击右上角按钮,跳转至当前微博电脑版/手机版

作者のサイトでサポートを受ける。または、このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
// ==UserScript==
// @name        新浪微博PC版手机版相互跳转
// @namespace   https://blog.bgme.me
// @match       https://m.weibo.cn/status/*
// @match       https://m.weibo.cn/detail/*
// @match       https://weibo.com/*/*
// @exclude     https://weibo.com/u/*
// @exclude     https://weibo.com/p/*
// @exclude     https://weibo.com/*/p/*
// @exclude     https://weibo.com/tv/*
// @exclude     https://weibo.com/signup/*
// @grant       none
// @run-at      document-end
// @version     1.1.3
// @author      bgme
// @description 点击右上角按钮,跳转至当前微博电脑版/手机版
// @supportURL  https://github.com/yingziwu/Greasemonkey/issues
// @icon        https://weibo.com/favicon.ico
// @license     AGPL-3.0-or-later
// ==/UserScript==

window.addEventListener('load', function () {
    addButton();
    setTimeout(testPage, 500);
})


function addButton() {
    let img = document.createElement('img');
    img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAG6wAABusBTDGeSgAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAeVSURBVHic7VpvTBNpHv6901L+yQU3nCfrByU9IRYrWrCVGkVzgu4ZCSFSMBDJrhcTP+iqMdlcLvFyl7sPftn9diZsiHterpYz8TxuCXsauBNy2VVoXIEIXV2hAYt/WqF/mMGZ6fzugwxXSjudlnYHWJ5kMm+n7/zmeZ55531/7ztDEBF+zKCUJqA01gxQmoDSWDNAaQJKY80ApQkojTUDlCawFNTU1Pzk3LlzzYQQkmiMFW0AABzRarVfXLx40UYISUjLijaA4zjNunXrQK/XWy5dutS5b98+dbwxVrQB4jwmIyMDtm/fXrV3796eQ4cOZcYTY0UbEAqNRgM6na7caDTeP3r0aI7c81aNAQAAaWlpsG3bNr3BYHhYW1v7npxzVpUBAABqtRoKCwu1er1+sK6uLj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01gxQmoDSWDNAaQJK40dvwP8AKk+/HC2PJW8AAAAASUVORK5CYII='
    img.style.cssText = 'height: 35px;	width: 25px;';

    let button = document.createElement('button');
    button.id = 'weibo-pc-mobile-switch';
    button.className = 'icon';
    button.style.cssText = `position: fixed;
                            top: 15%;
                            right: 5%;
                            z-index: 99;
                            border-style: none;
                            text-align:center;
                            vertical-align:baseline;
                            background-color: #fafafa;
                            border-radius: 50%;`
    button.onclick = function () {
        jump();
    };
    button.appendChild(img);
    document.body.appendChild(button);
}

function getWebUri() {
    let script;
    for (let s of document.querySelectorAll('body script')) {
        if (s.innerText.length && !s.hasAttribute('src')) {
            script = s;
            break;
        }
    }
    eval(script.innerText);
    const juid = $render_data.status.user.id;
    const jmid = $render_data.status.mid;

    let uid = juid;
    let mid = jmid;
    if (document.querySelector('a.m-img-box')) {
        const duid = document.querySelector('a.m-img-box').href.match(/\/(\d+)$/)[1];
        if (duid != juid) { uid = duid; }
    }
    if (document.URL.match(/^.*m\.weibo\.cn\/(status|detail)\/(\d+)\??.*$/i)) {
        const dmid = document.URL.match(/^.*m\.weibo\.cn\/(status|detail)\/(\d+)\??.*$/i)[2];
        if (dmid != jmid) { mid = dmid; }
    }

    let id = "";
    if (document.URL.match(/^.*m\.weibo\.cn\/(status|detail)\/(\w+)\??.*$/i) && !/^\d+$/.test(RegExp.$2)) {
        id = RegExp.$2;
    } else {
        id = WeiboUtil.mid2url(mid);
    }

    const href = `https://weibo.com/${uid}/${id}`;
    return href;
}

function getMobileUri() {
    const id = document.URL.split('?')[0].split('/')[4];

    let mid = "";
    if (document.querySelector('div.WB_cardwrap.WB_feed_type')) {
        mid = document.querySelector('div.WB_cardwrap.WB_feed_type').getAttribute('mid');
    } else {
        mid = WeiboUtil.url2mid(id);
    }

    const href = `https://m.weibo.cn/status/${mid}`;
    return href;
}

function getUrl() {
    let host = (new URL(document.URL)).host;
    switch (host) {
        case 'm.weibo.cn': {
            let webUri = getWebUri();
            return webUri;
        }
        case 'weibo.com': {
            let mobileUri = getMobileUri();
            return mobileUri;
        }
    }
}

function testPage() {
    try {
        getUrl();
    } catch (error) {
        console.error(error);
        document.querySelector('#weibo-pc-mobile-switch').style.setProperty('display', 'none');
    }
}

function jump() {
    let url = getUrl();
    console.log(url);
    window.open(url);
}

/**
 * 新浪微博mid与url互转实用工具
 * 作者: XiNGRZ (http://weibo.com/xingrz)
 * gist: https://gist.github.com/fengmk2/1844413
 */

var WeiboUtil = {
    // 62进制字典
    str62keys: [
        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
    ],
};

/**
 * 62进制值转换为10进制
 * @param {String} str62 62进制值
 * @return {String} 10进制值
 */
WeiboUtil.str62to10 = function (str62) {
    var i10 = 0;
    for (var i = 0; i < str62.length; i++) {
        var n = str62.length - i - 1;
        var s = str62[i];
        i10 += this.str62keys.indexOf(s) * Math.pow(62, n);
    }
    return String(i10);
};

/**
 * 10进制值转换为62进制
 * @param {String} int10 10进制值
 * @return {String} 62进制值
 */
WeiboUtil.int10to62 = function (int10) {
    var s62 = '';
    var r = 0;
    while (int10 != 0 && s62.length < 100) {
        r = int10 % 62;
        s62 = this.str62keys[r] + s62;
        int10 = Math.floor(int10 / 62);
    }
    return s62;
};

/**
 * URL字符转换为mid
 * @param {String} url 微博URL字符,如 "wr4mOFqpbO"
 * @return {String} 微博mid,如 "201110410216293360"
 */
WeiboUtil.url2mid = function (url) {
    var mid = '';

    for (var i = url.length - 4; i > -4; i = i - 4)	//从最后往前以4字节为一组读取URL字符
    {
        var offset1 = i < 0 ? 0 : i;
        var offset2 = i + 4;
        var str = url.substring(offset1, offset2);

        str = this.str62to10(str);
        if (offset1 > 0)	//若不是第一组,则不足7位补0
        {
            while (str.length < 7) {
                str = '0' + str;
            }
        }

        mid = str + mid;
    }

    return mid;
};

/**
 * mid转换为URL字符
 * @param {String} mid 微博mid,如 "201110410216293360"
 * @return {String} 微博URL字符,如 "wr4mOFqpbO"
 */
WeiboUtil.mid2url = function (mid) {
    if (!mid) {
        return mid;
    }
    mid = String(mid); //mid数值较大,必须为字符串!
    if (!/^\d+$/.test(mid)) { return mid; }
    var url = '';

    for (var i = mid.length - 7; i > -7; i = i - 7)	//从最后往前以7字节为一组读取mid
    {
        var offset1 = i < 0 ? 0 : i;
        var offset2 = i + 7;
        var num = mid.substring(offset1, offset2);

        num = this.int10to62(num);
        if (offset1 > 0) //若不是第一组,则不足4位补0
        {
            while (num.length < 4) {
                num = '0' + num;
            }
        }
        url = num + url;
    }

    return url;
};