Save URL to API(bilibili)

Click the "Save" button to send the current URL to the API.

  1. // ==UserScript==
  2. // @name Save URL to API(bilibili)
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description Click the "Save" button to send the current URL to the API.
  6. // @author You
  7. // @match https://www.bilibili.com/video/*
  8. // @grant GM_addStyle
  9. // @grant GM_xmlhttpRequest
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. GM_addStyle(`
  17. #bilibili-note-panel {
  18. position: absolute;
  19. z-index: 9999;
  20. background-color: #f0f0f0;
  21. color: #333;
  22. border: 1px solid #ccc;
  23. padding: 5px;
  24. border-radius: 8px;
  25. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  26. opacity: 0.9;
  27. min-width: 150px;
  28. max-width: 200px;
  29. transition: transform 0.2s ease, opacity 0.2s ease;
  30. }
  31.  
  32. #bilibili-note-panel.active {
  33. transform: scale(1.05);
  34. opacity: 1;
  35. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
  36. }
  37.  
  38. #bilibili-note-button {
  39. background-color: #FFB6C1;
  40. border: none;
  41. color: white;
  42. padding: 8px 16px;
  43. text-align: center;
  44. text-decoration: none;
  45. display: inline-block;
  46. font-size: 14px;
  47. margin: 0;
  48. cursor: pointer;
  49. border-radius: 6px;
  50. transition: background-color 0.3s ease;
  51. box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  52. white-space: nowrap;
  53. animation: button-pulse 1s infinite;
  54. }
  55.  
  56. #bilibili-note-button:hover {
  57. background-color: #FF80AB;
  58. }
  59.  
  60. @keyframes button-pulse {
  61. 0% { transform: scale(1); }
  62. 50% { transform: scale(1.02); }
  63. 100% { transform: scale(1); }
  64. }
  65. #save-url-button {
  66. background-color: #4CAF50; /* Green */
  67. border: none;
  68. color: white;
  69. padding: 8px 16px;
  70. text-align: center;
  71. text-decoration: none;
  72. display: inline-block;
  73. font-size: 14px;
  74. margin: 4px 2px;
  75. cursor: pointer;
  76. border-radius: 6px;
  77. }
  78. `);
  79.  
  80. // 创建面板
  81. const panel = document.createElement('div');
  82. panel.id = 'bilibili-note-panel';
  83. document.body.appendChild(panel);
  84.  
  85. // 创建 "Save" 按钮
  86. const saveButton = document.createElement('button');
  87. saveButton.id = 'save-url-button';
  88. saveButton.textContent = 'Save URL';
  89. panel.appendChild(saveButton);
  90.  
  91. // 设置面板初始位置
  92. panel.style.top = '10px';
  93. panel.style.right = '10px';
  94.  
  95. // 点击 "Save" 按钮事件
  96. saveButton.addEventListener('click', function() {
  97. const currentUrl = window.location.href;
  98.  
  99. // 构建要发送的数据, 新增种类 bilibiliurl
  100. const data = {
  101. "api_endpoint": "url", // 你可以根据需要修改这个值
  102. "fixed_value": {
  103. "url": currentUrl,
  104. "type": "bilibili_latest_Episode"
  105. }
  106. };
  107.  
  108. // 发送 POST 请求到 API
  109. GM_xmlhttpRequest({
  110. method: "POST",
  111. url: "http://localhost:8964/update_api", // API 服务器地址
  112. headers: {
  113. "Content-Type": "application/json"
  114. },
  115. data: JSON.stringify(data),
  116. onload: function(response) {
  117. console.log("API response:", response.responseText);
  118. if (response.status >= 200 && response.status < 300) {
  119. saveButton.textContent = 'Saved!';
  120. setTimeout(() => {
  121. saveButton.textContent = 'Save URL';
  122. }, 2000);
  123. } else {
  124. saveButton.textContent = 'Error!';
  125. setTimeout(() => {
  126. saveButton.textContent = 'Save URL';
  127. }, 2000);
  128. }
  129. },
  130. onerror: function(error) {
  131. console.error("API request failed:", error);
  132. saveButton.textContent = 'Error!';
  133. setTimeout(() => {
  134. saveButton.textContent = 'Save URL';
  135. }, 2000);
  136. }
  137. });
  138. });
  139. })();