Web按鈕注入

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

Verze ze dne 21. 12. 2023. Zobrazit nejnovější verzi.

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/453745/1299382/Web%E6%8C%89%E9%88%95%E6%B3%A8%E5%85%A5.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==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