B站身份码一键获取

在页面左上角添加一个可拖动的按钮,点击以获取并复制Bilibili身份码,修复了拖动后误触发复制的问题

  1. // ==UserScript==
  2. // @name B站身份码一键获取
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description 在页面左上角添加一个可拖动的按钮,点击以获取并复制Bilibili身份码,修复了拖动后误触发复制的问题
  6. // @author 与歌一生
  7. // @match *://*.bilibili.com/*
  8. // @grant GM_setClipboard
  9. // @require https://cdn.jsdelivr.net/npm/sweetalert2@11
  10. // @run-at document-end
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // 创建悬浮按钮并添加样式及拖动功能
  18. const button = document.createElement('button');
  19. button.style.position = 'fixed';
  20. button.style.top = '120px';
  21. button.style.left = '10px';
  22. button.style.zIndex = '9999';
  23. button.style.padding = '5px 10px';
  24. button.style.border = 'none';
  25. button.style.backgroundColor = '#ADD8E6';
  26. button.style.color = 'white';
  27. button.style.borderRadius = '5px';
  28. button.textContent = '获取身份码';
  29. document.body.appendChild(button);
  30.  
  31. let isDragging = false;
  32. let offsetX, offsetY;
  33.  
  34. button.addEventListener('mousedown', (e) => {
  35. isDragging = true;
  36. offsetX = e.clientX - button.offsetLeft;
  37. offsetY = e.clientY - button.offsetTop;
  38. document.addEventListener('mousemove', onMouseMove);
  39. document.addEventListener('mouseup', onMouseUp);
  40. });
  41.  
  42. function onMouseMove(e) {
  43. if (!isDragging) return;
  44. button.style.left = `${e.clientX - offsetX}px`;
  45. button.style.top = `${e.clientY - offsetY}px`;
  46. button.style.pointerEvents = 'none'; // 禁用点击事件
  47. }
  48.  
  49. function onMouseUp() {
  50. isDragging = false;
  51. document.removeEventListener('mousemove', onMouseMove);
  52. document.removeEventListener('mouseup', onMouseUp);
  53. button.style.pointerEvents = ''; // 重新启用点击事件
  54. }
  55.  
  56. button.addEventListener('click', async () => {
  57. if (isDragging) return; // 如果正在拖动,则不执行点击事件
  58. try {
  59. const uri = 'https://api.live.bilibili.com/xlive/open-platform/v1/common/operationOnBroadcastCode';
  60. const csrf_token = /bili_jct=([a-zA-Z0-9]*)/.exec(document.cookie)[1];
  61. const headers = {
  62. "Content-Type": "application/x-www-form-urlencoded",
  63. "Sec-Fetch-Mode": "cors"
  64. };
  65. const referrer = 'https://link.bilibili.com/p/center/index';
  66.  
  67. const res = await fetch(uri, {
  68. headers,
  69. referrer,
  70. body: `action=1&csrf_token=${csrf_token}&csrf=${csrf_token}`,
  71. method: "POST",
  72. mode: "cors",
  73. credentials: "include"
  74. });
  75.  
  76. const result = await res.json();
  77. if (result.code === 0 && result.data && result.data.code) {
  78. await GM_setClipboard(result.data.code);
  79. if (typeof Swal === 'function') {
  80. Swal.fire({
  81. icon: 'success',
  82. title: '成功',
  83. text: '身份码已复制到剪贴板!',
  84. showConfirmButton: false,
  85. timer: 1500
  86. });
  87. } else {
  88. alert('身份码已复制到剪贴板!');
  89. }
  90. } else {
  91. if (typeof Swal === 'function') {
  92. Swal.fire({
  93. icon: 'error',
  94. title: '错误',
  95. text: '获取身份码失败,请检查网络或重试!',
  96. });
  97. } else {
  98. alert('获取身份码失败,请检查网络或重试!');
  99. }
  100. }
  101. } catch (error) {
  102. console.error('发生错误:', error);
  103. if (typeof Swal === 'function') {
  104. Swal.fire({
  105. icon: 'error',
  106. title: '错误',
  107. text: '发生了未知错误!',
  108. });
  109. } else {
  110. alert('发生了未知错误!');
  111. }
  112. }
  113. });
  114.  
  115. // 确保SweetAlert2已加载
  116. if (typeof Swal === 'undefined') {
  117. const script = document.createElement('script');
  118. script.src = 'https://cdn.jsdelivr.net/npm/sweetalert2@11';
  119. document.head.appendChild(script);
  120. }
  121. })();

QingJ © 2025

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