bilibili 移动端

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

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

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

})();