【问题标题】:Add a JavaScript button using Greasemonkey or Tampermonkey?使用 Greasemonkey 或 Tampermonkey 添加 JavaScript 按钮?
【发布时间】:2011-09-22 17:33:46
【问题描述】:

我对 Greasemonkey 的世界还很陌生,我想知道如何在 JavaScript 中制作一个按钮。

假设我想在 YouTube 或 Google 上放置一个按钮?我将如何调用它或制作它?

我很困惑,找不到任何东西。除非有办法与这些网站的 HTML 交互并将它们添加到 Greasemonkey 脚本中?

【问题讨论】:

  • 很难相信您找不到任何东西。 Internet 上一定有 几十个 Greasemonkey 教程。

标签: javascript button greasemonkey tampermonkey


【解决方案1】:

好的,这是一个向 SO 问题页面添加实时按钮的完整脚本1

// ==UserScript==
// @name        _Adding a live button
// @description Adds live example button, with styling.
// @match       *://stackoverflow.com/questions/*
// @match       *://YOUR_SERVER.COM/YOUR_PATH/*
// @grant       GM_addStyle
// ==/UserScript==

/*--- Create a button in a container div.  It will be styled and
    positioned with CSS.
*/
var zNode       = document.createElement ('div');
zNode.innerHTML = '<button id="myButton" type="button">'
                + 'For Pete\'s sake, don\'t click me!</button>'
                ;
zNode.setAttribute ('id', 'myContainer');
document.body.appendChild (zNode);

//--- Activate the newly added button.
document.getElementById ("myButton").addEventListener (
    "click", ButtonClickAction, false
);

function ButtonClickAction (zEvent) {
    /*--- For our dummy action, we'll just add a line of text to the top
        of the screen.
    */
    var zNode       = document.createElement ('p');
    zNode.innerHTML = 'The button was clicked.';
    document.getElementById ("myContainer").appendChild (zNode);
}

//--- Style our newly added elements using CSS.
GM_addStyle ( `
    #myContainer {
        position:               absolute;
        top:                    0;
        left:                   0;
        font-size:              20px;
        background:             orange;
        border:                 3px outset black;
        margin:                 5px;
        opacity:                0.9;
        z-index:                1100;
        padding:                5px 20px;
    }
    #myButton {
        cursor:                 pointer;
    }
    #myContainer p {
        color:                  red;
        background:             white;
    }
` );




1 令人惊讶的是,以前在 SO 上似乎并没有这样问过这个问题。

【讨论】:

  • 我想知道是否有办法改变按钮的背景图像。
  • @YoussofHammoud,您可以使用 CSS,例如:background: url("https://interactive-examples.mdn.mozilla.net/media/examples/lizard.png");
  • 在 GreaseMonkey 4 及更高版本中 addStyle 已被移除,因此您需要通过直接设置元素的属性来设置位置。
【解决方案2】:

如果你问我,你可以做得更小(使用 HTML5 n es6),比如:

function addButton(text, onclick, cssObj) {
    cssObj = cssObj || {position: 'absolute', bottom: '7%', left:'4%', 'z-index': 3}
    let button = document.createElement('button'), btnStyle = button.style
    document.body.appendChild(button)
    button.innerHTML = text
    button.onclick = onclick
    btnStyle.position = 'absolute'
    Object.keys(cssObj).forEach(key => btnStyle[key] = cssObj[key])
    return button
}

示例脚本(用于选择谷歌收件箱中所有已读邮件):

// ==UserScript==
// @name        mark unread
// @namespace   all
// @include     https://inbox.google.com/*
// @version     1
// @grant       none
// ==/UserScript==

(function(){
    'use strict'

  window.addEventListener('load', () => {
    addButton('select read', selectReadFn)
    })

    function addButton(text, onclick, cssObj) {
        cssObj = cssObj || {position: 'absolute', bottom: '7%', left:'4%', 'z-index': 3}
        let button = document.createElement('button'), btnStyle = button.style
        document.body.appendChild(button)
        button.innerHTML = text
        button.onclick = onclick
        Object.keys(cssObj).forEach(key => btnStyle[key] = cssObj[key])
        return button
    }

    function selectReadFn() {
        [...document.getElementsByClassName('MN')].filter(isRead).forEach(element => element.click())
    }

    function isRead(element) {
        childs = element.parentElement.parentElement.parentElement.getElementsByClassName('G3')
        return ![...childs].some(e => e.innerText.search(/unread/i)!==-1)
    }

}())

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    相关资源
    最近更新 更多