bilibili 移动端

b 站移动端网页推荐视频直接看

当前为 2023-06-08 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name               Bilibili Mobile
// @name:zh-CN         bilibili 移动端
// @namespace          https://github.com/jk278/bilibili-mobile
// @version            1.1
// @description        view bilibili mobile page recommended video  directly 
// @description:zh-CN  b 站移动端网页推荐视频直接看
// @author             jk278
// @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);
        console.log('Execute Video!');
    }

    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);

        }
        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);
    }

    function run() {
        let path = window.location.pathname;
        const urlSearchParams = new URLSearchParams(window.location.search);

        if (path.startsWith('/video')) {
            runVideo();
        } else if (path.startsWith('/search') && urlSearchParams.get('utm_source') === 'userscript') {
            runSearch();
        }
    }

    run();

})();