如何为Greasemonkey脚本创建切换按钮?

时间:2012-06-19 20:18:34

标签: javascript button greasemonkey

所以我试图弄清楚如何为运行并导航到各个页面的greasemonkey扩展创建一个切换按钮。

所以这就是我到目前为止的作品

var keepgoing = true

if语句和内容的开头

else if keepgoing == true
  { newsearch(); }

按钮点击:

 if keepgoing == true { keepgoing = false }
 else if keepgoing == false { keepgoing = true }

所以基本上我需要帮助在网页上放一个按钮。 并且它需要在浏览页面时记住var。

谢谢,Ray

1 个答案:

答案 0 :(得分:0)

如果您想要持久性按钮(或任何数据),则需要使用某种存储。如果涉及多个域,请使用GM_setValue()。如果所有内容都在同一个域中,请使用localStorage

我自己添加了这种持久性按钮,这是一个简单的向下脚本,显示我使用的内容。它不只是添加一个按钮,它使按钮UI更加用户友好,IMO。

注意:

  1. 按钮状态会在页面加载和网站间持续存在。
  2. 按钮位于左上方(由CSS设置),并且在未被遮盖时淡入近乎不透明状态。
  3. 我使用了一个对象,因为我有时会将多个控件添加到页面中。
  4. 你可以see a demo of how the button appears and behaves at jsFiddle。 (除了这些值不能在演示中的页面加载中持续存在。它们在GM脚本中执行。)

  5. // ==UserScript==
    // @name     _Keep going button
    // @include  http://YOUR_SERVER_1.COM/YOUR_PATH/*
    // @include  http://YOUR_SERVER_2.COM/YOUR_PATH/*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
    // ==/UserScript==
    
    //--- Add the button.
    $("body").append (
          '<div class="gmPersistentButton">'
        + '<button id="gmContinueBtn">Init failed!</button></div>'
    );
    
    //--- Define and init the matching control object:
    var btnControl  = new PersistentButton (
        "gmContinueBtn",        //-- HTML id
        "StopContinueBtn",      //-- Storage label
        ["Stop", "Continue"],   //-- Text that the button cycles through
        [false, true]           //-- Matching values for the button's states
    );
    
    //--- Activate the button click-handler.
    $("#gmContinueBtn").click ( function () {
        var btnValue    = this.value;
        keepgoing       = btnValue
        btnControl.SetNextValue ();
    
        if (keepgoing == "true") {
            // CALL YOUR FUNCTION HERE.
        }
    } );
    
    //--- The button will fade when we aren't using it.
    var zDisplayPanel   = $('div.gmPersistentButton');
    zDisplayPanel.hover (
        function () { $(this).stop (true, false).fadeTo (50,  1  ); },
        function () { $(this).stop (true, false).fadeTo (900, 0.1); }
    );
    zDisplayPanel.fadeTo (2900, 0.1);
    
    
    //--- CSS styles to make it work and to improve appearance.
    GM_addStyle ( (<><![CDATA[
        .gmPersistentButton {
            background:         orange;
            color:              black;
            position:           fixed;
            top:                1em;
            right:              1em;
            z-index:            6666;
            padding:            1em;
            border:             3px double gray;
            border-radius:      1ex;
            opacity:            0.8;
        }
        .gmPersistentButton button {
            cursor:             pointer;
        }
        .gmPersistentButton button:hover {
            color:              red;
        }
    ]]></>).toString () );
    
    
    //--- Button object
    function PersistentButton (htmlID, setValName, textArry, valueArry) {
        //--- Initialize the button to last stored value or default.
        var buttonValue     = valueArry[0];
        fetchValue ();
        storeValue ();      //-- Store, in case it wasn't already.
        setButtonTextAndVal ();
    
        //--- DONE with init.  Set click and keyboard listeners externally.
    
        //***** Public functions:
        this.Reset          = function () {
            buttonValue     = valueArry[0];
            storeValue ();    
            setButtonTextAndVal ();
        };
    
        this.SetNextValue   = function () {
            var numValues   = valueArry.length;
            var valIndex    = 0;
    
            for (var J = numValues - 1;  J >= 0;  --J) {
                if (buttonValue == valueArry[J]) {
                    valIndex    = J;
                    break;
                }
            }
            valIndex++;
            if (valIndex >= numValues)
                valIndex    = 0;
    
            buttonValue     = valueArry[valIndex];
    
            storeValue ();    
            setButtonTextAndVal ();
        };
    
    
        //***** Private functions:
        function fetchValue () {
            buttonValue     = GM_getValue (setValName, buttonValue);
        }
    
        function storeValue () {
            GM_setValue (setValName, buttonValue);
        }
    
        function setButtonTextAndVal () {
            var buttonText  = "*ERROR!*";
    
            for (var J = valueArry.length - 1;  J >= 0;  --J) {
                if (buttonValue == valueArry[J]) {
                    buttonText  = textArry[J];
                    break;
                }
            }
    
            var theBtn      = document.getElementById (htmlID);
            if (theBtn) {
                theBtn.textContent  = buttonText;
                theBtn.setAttribute ("value", buttonValue);
            }
            else
                alert ('Missing persistent button with ID: ' + htmlID + '!');
        }
    }