Greasy Fork is available in English.

Web按鈕注入

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

Este script não deve ser instalado diretamente. Este script é uma biblioteca de outros scripts para incluir com o diretório meta // @require https://update.greasyfork.org/scripts/453745/1299382/Web%E6%8C%89%E9%88%95%E6%B3%A8%E5%85%A5.js

// ==UserScript==
// @name         Web按鈕注入
// @namespace    
// @version      2.0.0
// @description  向頁面批量注入按鈕並進行函數綁定
// @author       otc
// @match        *
// @license MIT
// ==/UserScript==

function createButtons(buttons) {  
  // 遍历按钮列表  
  buttons.forEach((button, index) => {  
    // 创建按钮元素  
    const buttonElement = document.createElement('button');  
  
    // 设置按钮的文本内容  
    buttonElement.textContent = button.name;  
  
    // 将按钮添加到页面中  
    document.body.appendChild(buttonElement);  

    // 设置按钮的样式 
    buttonElement.style.zIndex = 10000;  
    buttonElement.style.position = 'absolute';  
    buttonElement.style.top = index*20+'px'; 
    buttonElement.style.left = 20+'px'; 
    
  
    // 将按钮的点击事件与对应的函数关联起来  
    buttonElement.addEventListener('click', button.func);  
  });  
}  

// #region例子:
// function funca (){
//   console.log("funca");
// }
// function funcb (){
//   console.log("funcb");
// }
  
// // 调用 createButtons 函数创建按钮,传入一个包含函数和按钮名的列表  
// const buttons = [  
//   { name: 'funca', func: funca },  
//   { name: 'funcb', func: funcb }  
// ];  
// createButtons(buttons);
// #endregion