Bilibili哔哩哔哩快捷复制视频链接

快速复制bilibili 用户空间的视频列表视频链接

当前为 2024-05-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         Bilibili哔哩哔哩快捷复制视频链接
// @namespace    http://tampermonkey.net/
// @description  快速复制bilibili 用户空间的视频列表视频链接
// @author       You
// @match        https://space.bilibili.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @grant        none
// @version 1.0.2
// @license MIT
// ==/UserScript==
(function () {
  "use strict";
  let count = 0;

  function checkLoaded() {
    // 超过30次检测还没有就不要检测了
    if (count >= 30) {
      console.error(
        "Bilibili哔哩哔哩快捷复制视频链接\n 未检测到目标元素!已停止检测"
      );
      return;
    }

    const titles = document.querySelectorAll(".small-item .title");
    count++;
    if (titles.length === 0) {
      setTimeout(() => {
        checkLoaded();
        console.log(
          `Bilibili哔哩哔哩快捷复制视频链接\n:check if dom mounted...${count}times`
        );
      }, 400);
    } else {
      run();
    }
  }

  function run() {
    const titles = document.querySelectorAll(".small-item .title");
    const copyInitText = "(👉゚ヮ゚)👉 Click To Copy ~";
    const successText = "Copy Success 🥦⭕";
    const failText = "Copy Fail 🍓❌";
    titles.forEach((title) => {
      title.parentNode.insertBefore(
        (() => {
          const span = document.createElement("span");
          span.style = `
    font-size: 10px;
    padding: 0 6px;
    margin: 2px 0;
    border-radius: 6px;
    display: inline-block;
    background-color: #000;
    color: rgb(251, 114, 153);
    cursor: pointer;
    position:absolute;
`;
          title.style.marginTop = "20px";
          span.title = title.href;
          span.textContent = copyInitText;
          span.addEventListener("click", () => {
            copyTextToClipboard(span.title).then((success) => {
              if (success) {
                span.textContent = successText;
                span.style.color = "#00d2a5";
              } else {
                span.textContent = failText;
                span.style.color = "#ff0049";
              }
              const timer = setTimeout(() => {
                span.textContent = copyInitText;
                span.style.color = "#fb7299";
                clearTimeout(timer);
              }, 1000);
            });
          });
          return span;
        })(),
        title
      );
    });

    async function copyTextToClipboard(text) {
      return new Promise((resolve, reject) => {
        if (!navigator.clipboard) {
          console.log("Clipboard API not available");
          resolve(false);
        }
        navigator.clipboard
          .writeText(text)
          .then(function () {
            console.log("Text copied to clipboard");
            resolve(true);
          })
          .catch(function (err) {
            resolve(false);
            console.error("Could not copy text: ", err);
          });
      });
    }
  }

  checkLoaded();
  navigation.addEventListener("navigate", () => {
    console.log("page changed");
    count=0

    setTimeout(checkLoaded,1500)

  });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址