Web按鈕注入

向頁面批量注入按鈕並進行函數綁定

目前为 2025-01-23 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/453745/1525479/Web%E6%8C%89%E9%88%95%E6%B3%A8%E5%85%A5.js

  1. // ==UserScript==
  2. // @name Web按鈕注入
  3. // @namespace
  4. // @version 2.0.0
  5. // @description 向頁面批量注入按鈕並進行函數綁定
  6. // @author otc
  7. // @match *
  8. // @license MIT
  9. // ==/UserScript==
  10.  
  11. // 设置容器默认样式
  12. function getDefaultContainerStyles() {
  13. return {
  14. position: 'fixed',
  15. top: '0',
  16. left: '0',
  17. display: 'flex',
  18. flexDirection: 'column',
  19. justifyContent: 'center',
  20. alignItems: 'center',
  21. width: '5px', // 默认宽度
  22. height: '100%', // 全屏高度
  23. backgroundColor: 'rgba(0, 0, 0, 0.1)', // 半透明背景
  24. transition: 'width 0.3s ease', // 动画效果
  25. overflow: 'hidden', // 隐藏超出部分
  26. zIndex: 10000,
  27. };
  28. }
  29.  
  30. // 创建或获取容器
  31. function getOrCreateButtonContainer() {
  32. let container = document.querySelector('.ocr-buttons-container');
  33. if (!container) {
  34. container = document.createElement('div');
  35. container.className = 'ocr-buttons-container';
  36.  
  37. // 应用默认样式
  38. const styles = getDefaultContainerStyles();
  39. for (const [key, value] of Object.entries(styles)) {
  40. container.style[key] = value;
  41. }
  42.  
  43. // 创建删除按钮
  44. const deleteButton = document.createElement('button');
  45. deleteButton.className = 'delete-button';
  46. deleteButton.innerHTML = '<span class="button-text">×</span>'; // 使用 span 包裹文字
  47. deleteButton.style.position = 'absolute';
  48. deleteButton.style.top = '10px';
  49. deleteButton.style.right = '-30px'; // 初始位置在容器外
  50. deleteButton.style.padding = '5px 10px';
  51. deleteButton.style.fontSize = '14px';
  52. deleteButton.style.border = 'none';
  53. // deleteButton.style.background = 'rgba(255, 255, 255, 0.8)';
  54. deleteButton.style.background = 'rgba(222, 55, 48, 0.8)';
  55. deleteButton.style.color = '#fff';
  56. deleteButton.style.borderRadius = '5px';
  57. deleteButton.style.cursor = 'pointer';
  58. deleteButton.style.transition = 'right 0.3s ease';
  59.  
  60. deleteButton.addEventListener('click', () => {
  61. container.remove(); // 删除整个容器
  62. });
  63.  
  64. container.appendChild(deleteButton);
  65.  
  66. // 鼠标悬停事件
  67. container.addEventListener('mouseenter', () => {
  68. container.style.width = '150px'; // 展开宽度
  69. deleteButton.style.right = '10px'; // 显示删除按钮
  70. container.querySelectorAll('.button-text').forEach(text => {
  71. text.style.opacity = 1; // 显示文字
  72. });
  73. clearTimeout(container.hideTimeout); // 清除之前的定时器
  74. });
  75.  
  76. // 鼠标移出事件
  77. container.addEventListener('mouseleave', () => {
  78. container.hideTimeout = setTimeout(() => {
  79. container.style.width = '5px'; // 收起宽度
  80. deleteButton.style.right = '-30px'; // 隐藏删除按钮
  81. container.querySelectorAll('.button-text').forEach(text => {
  82. text.style.opacity = 0; // 隐藏文字
  83. });
  84. }, 1000); // 1秒后收起
  85. });
  86.  
  87. document.body.appendChild(container);
  88. }
  89. return container;
  90. }
  91.  
  92. // 创建按钮
  93. function createButtons(buttons) {
  94. const container = getOrCreateButtonContainer();
  95.  
  96. buttons.forEach((button, index) => {
  97. const buttonElement = document.createElement('button');
  98. buttonElement.innerHTML = `<span class="button-text">${button.name}</span>`; // 使用 span 包裹文字
  99. buttonElement.style.margin = '2px 0';
  100. buttonElement.style.width = '140px';
  101. buttonElement.style.border = 'none';
  102. buttonElement.style.background = '#007bff';
  103. buttonElement.style.color = '#fff';
  104. buttonElement.style.borderRadius = '4px';
  105. buttonElement.style.fontSize = '14px';
  106. buttonElement.style.cursor = 'pointer';
  107. buttonElement.style.transition = 'background 0.2s ease';
  108.  
  109. // 隐藏文字的样式
  110. const buttonText = buttonElement.querySelector('.button-text');
  111. buttonText.style.opacity = 0; // 默认隐藏文字
  112. buttonText.style.transition = 'opacity 0.3s ease';
  113.  
  114. buttonElement.addEventListener('mouseover', () => {
  115. buttonElement.style.background = '#0056b3';
  116. });
  117. buttonElement.addEventListener('mouseout', () => {
  118. buttonElement.style.background = '#007bff';
  119. });
  120.  
  121. buttonElement.addEventListener('click', button.func);
  122.  
  123. container.appendChild(buttonElement);
  124. });
  125. }
  126.  
  127. // #region例子:
  128. // function funca (){
  129. // console.log("funca");
  130. // }
  131. // function funcb (){
  132. // console.log("funcb");
  133. // }
  134. // // 调用 createButtons 函数创建按钮,传入一个包含函数和按钮名的列表
  135. // const buttons = [
  136. // { name: 'funca', func: funca },
  137. // { name: 'funcb', func: funcb }
  138. // ];
  139. // createButtons(buttons);
  140. // #endregion

QingJ © 2025

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