javascript - 如何在 Greasemonkey 脚本中弹出自定义表单/对话框?

标签 javascript forms dialog popup greasemonkey

我一直在研究一个在 Firefox 中用作插件的脚本,并且发现需要在单击某个按钮时弹出自定义表单。

我需要能够自己创建整个表单,然后解析输入的数据以在原始站点上呈现图像。

我该怎么做?

最佳答案

好的,这是一个完整的脚本示例,展示了如何弹出一个表单并与其控件进行交互。
请注意,它使用 jQuery -- 这使它变得更容易/更短/更简单。

// ==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;          \
    }                                                           \
" );



您会注意到该对话框非常简单。对于更复杂的表格,您可以使用 jQuery-UI .

关于javascript - 如何在 Greasemonkey 脚本中弹出自定义表单/对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11668111/

相关文章:

javascript - 在 jQuery Grid 中单击时无法选中标题复选框

javascript - 如何在 AngularJS 中的 Angular 数据表(l-lin)上应用 DOM 外部的button().trigger()

javascript - JQuery on change 以一种形式而不是另一种形式工作

android - 从应用程序填写网络表格并提交

javascript - 商店谁提交了表格?

javascript - 通过 python 使用 seleniumexecute_script 时删除转义字符

javascript - 从表单javascript传递变量值

java - 当我第二次运行对话框时,我的 Android 应用程序崩溃了

android - 即使屏幕被锁定,如何显示对话框?

flutter - 如何更改 CupertinoAlertDialog 的背景颜色?