How do I pop up a custom form/dialog in a Greasemonkey script?

前端 未结 1 1913
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 04:09

I have been working on a script to use as a plugin in Firefox, and have come across the need to pop up a custom form when a certain button is clicked.

I need to be

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-01 04:44

    Okay, here is a complete script example, showing how to pop up a form and interact with its controls.
    Note that it uses jQuery -- which makes it vastly easier/shorter/simpler.

    // ==UserScript==
    // @name        _Form, popup example
    // @include     http://stackoverflow.com/*
    // @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
    // @grant       GM_addStyle
    // ==/UserScript==
    /*- The @grant directive is needed to work around a design change
        introduced in GM 1.0.   It restores the sandbox.
    */
    
    //--- Use jQuery to add the form in a "popup" dialog.
    $("body").append ( '                                                          \
        
    \
    \ \ \ \

     

    \ \ \
    \
    \ ' ); //--- Use jQuery to activate the dialog buttons. $("#gmAddNumsBtn").click ( function () { var A = $("#myNumber1").val (); var B = $("#myNumber2").val (); var C = parseInt(A, 10) + parseInt(B, 10); $("#myNumberSum").text ("The sum is: " + C); } ); $("#gmCloseDlgBtn").click ( function () { $("#gmPopupContainer").hide (); } ); //--- CSS styles make it work... GM_addStyle ( " \ #gmPopupContainer { \ position: fixed; \ top: 30%; \ left: 20%; \ padding: 2em; \ background: powderblue; \ border: 3px double black; \ border-radius: 1ex; \ z-index: 777; \ } \ #gmPopupContainer button{ \ cursor: pointer; \ margin: 1em 1em 0; \ border: 1px outset buttonface; \ } \ " );



    You'll note that the dialog is pretty basic. For more sophisticated forms, you can use jQuery-UI.

    0 讨论(0)
提交回复
热议问题