Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

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

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 able to create the entire form myself and then parse the entered data to render images on the original site.

How do I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ( '                                                          
    <div id="gmPopupContainer">                                               
    <form> <!-- For true form use method="POST" action="YOUR_DESIRED_URL" --> 
        <input type="text" id="myNumber1" value="">                           
        <input type="text" id="myNumber2" value="">                           
                                                                              
        <p id="myNumberSum">&nbsp;</p>                                        
        <button id="gmAddNumsBtn" type="button">Add the two numbers</button>  
        <button id="gmCloseDlgBtn" type="button">Close popup</button>         
    </form>                                                                   
    </div>                                                                    
' );


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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...