B站视频在线观看人数

添加一个跳转在线播放视频的按钮

// ==UserScript==
// @name         B站视频在线观看人数
// @namespace    http://tampermonkey.net/
// @version      1.1.0
// @description  添加一个跳转在线播放视频的按钮
// @author       Kirari
// @match        https://www.bilibili.com/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=openuserjs.org
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    var url = [
        "https://www.bilibili.com/video/online.html",
        "https://www.bilibili.com/video/online.html?spm_id_from=333.851.b_7265706f7274466972737432.14"
    ];
    var randomIndex = Math.floor(Math.random() * url.length);
    var randomUrl = url[randomIndex];
    var newDiv = document.createElement("div");
    newDiv.id = "online_button_container";
    var newButton = document.createElement("button");
    newButton.textContent = "在线人数";

    newButton.onclick = function() {
        window.open(randomUrl, '_blank');
    };

    newDiv.appendChild(newButton);

   var browserWidth = window.innerWidth;
    if(browserWidth>1560){
      newDiv.style.width= '70px';
      newDiv.style.padding= '16px';
      newDiv.style.font='16px';
      newDiv.style.borderRadius='25px';
      newDiv.style.left = browserWidth-75+'px';
    }else{
        newDiv.style.width='55px';
        newDiv.style.height= '55px';
      newDiv.style.padding= '8px';
      newDiv.style.left = browserWidth-40+'px';
    }


    newDiv.style.position = 'sticky';
    newDiv.style.bottom = '30px';

    newDiv.style.zIndex = '1000';
    newDiv.style.border = 'solid 1px black';


    var parentElement = document.body;

    parentElement.appendChild(newDiv);

    var style = document.createElement('style');
    style.innerHTML = `
   @media(min-width:1560px){
    width: 70px;
    padding: 16px;
      font:16px;
      border-radius:25px;
    transition: top 1s ease;
  }
  @media(max-width:1560px){
      width: 40px;
    height: 40px;
    transition: top 1s ease;
  }
  @media (min-width: 601px) {
    #online_button_container {
      bottom: 10px;
      right: 10px;
    }
    #online_button_container button {

    }
  }
`;
    document.head.appendChild(style);
  window.addEventListener('scroll', () => {
            // 获取当前滚动的像素值
            var scrollY = window.scrollY || document.documentElement.scrollTop;
            if(scrollY>=700 && browserWidth<=1560){
              newDiv.style.bottom='220px';
            }else{
               newDiv.style.bottom='30px';
            }
            // 更新显示的滚动信息

        });
window.addEventListener('resize', ()=>{
   browserWidth = window.innerWidth;
  if(browserWidth>1560){
    newDiv.style.width= '70px';
    newDiv.style.height= '70px';
    newDiv.style.padding= '16px';
      newDiv.style.font='16px';
      newDiv.style.borderRadius='25px';
    newDiv.style.left = browserWidth-75+'px';
  }else{
      newDiv.style.width='55px';
      newDiv.style.height= '55px';
    newDiv.style.padding= '8px';
    newDiv.style.left = browserWidth-40+'px';
  }
});
    // Your code here...
})();