Bilibili Mobile

view bilibili mobile page recommended video directly

目前為 2023-06-09 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name               Bilibili Mobile
// @name:zh-CN         bilibili 移动端
// @namespace          https://github.com/jk278/bilibili-mobile
// @version            1.1.2
// @description        view bilibili mobile page recommended video  directly 
// @description:zh-CN  b 站移动端网页推荐视频直接看
// @author             jk 278
// @match              *://m.bilibili.com
// @match              *://m.bilibili.com/video/*
// @match              *://m.bilibili.com/search?*
// @icon               https://www.bilibili.com/favicon.ico
// @require            http://code.jquery.com/jquery-3.6.0.min.js
// ==/UserScript==

(function () {
    'use strict';

    function runVideo() {

        function addTargetElementListener(targetElement) {
            if (targetElement) {
                const anchor = targetElement.firstChild;
                const h2Element = anchor.lastChild;

                const keyword = encodeURIComponent(h2Element.innerHTML);
                const searchUrl = `https://m.bilibili.com/search?keyword=${keyword}&utm_source=userscript`;

                anchor.addEventListener(
                    'click',
                    async (event) => {
                        event.preventDefault();
                        event.stopImmediatePropagation();

                        await new Promise((resolve) => setTimeout(resolve, 0));
                        window.location.href = searchUrl;
                    },
                    true
                );
            }
        }

        document.querySelector('.player-icon').click();
        document.querySelector('.play-icon').click();

        let timer = setInterval(function () {
            const dialog = document.querySelector('.dialog');
            if (dialog) {
                clearInterval(timer);
                dialog.lastChild.click();
            }
        }, 200);

        const cardBox = document.querySelector('.card-box');
        const targetElements = cardBox.children;

        // 为初始子元素添加监听器
        Array.from(targetElements).forEach(addTargetElementListener);

        // 创建 MutationObserver 以监听子元素的变化
        const observer = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                if (mutation.type === 'childList') {
                    mutation.addedNodes.forEach((addedNode) => {
                        addTargetElementListener(addedNode);
                    });
                }
            });
        });

        // 配置观察选项
        const observerConfig = {
            childList: true,
        };

        // 开始观察
        observer.observe(cardBox, observerConfig);
    }

    function runSearch() {
        const searchResultElement = document.querySelector('.v-card-single'); // 第一个
        if (searchResultElement) {
            // clearInterval(timer);
            searchResultElement.click();

            let dialogTimer = setInterval(function () {
                const vDialog = document.querySelector('.v-dialog');
                if (vDialog) {
                    clearInterval(dialogTimer);
                    vDialog.querySelector('.cancel').click();
                }
            }, 200);

        }
    }

    if (window.location.pathname.startsWith('/video')) {
        runVideo();
        console.log('Execute Video!');
    } else if (window.location.pathname.startsWith('/search')
        && new URLSearchParams(window.location.search).get('utm_source') === 'userscript') {
        runSearch();
        console.log('Execute Search!');
    }

    const href = window.location.href;
    let hrefTimer = setInterval(function () {
        if (window.location.href !== href) {
            clearInterval(hrefTimer);
            window.location.href = window.location.href;
        }
    }, 200);

})();