使用Greasemonkey和JQuery将动态表单添加到特定页面

时间:2010-06-26 22:15:11

标签: javascript jquery greasemonkey

在特定论坛中,单击“回复”按钮会生成一个带有文本表单的新窗口以键入回复。我想实现一个脚本来在主页面中创建特定的文本表单(而不是生成一个新窗口)。我该怎么做呢?

以下是我要在其上实现脚本的页面的源代码:

http://pastebin.com/2UaUVGJA(主要讨论页面) http://pastebin.com/hAx2SPUu(回复页面)

这是尝试过的脚本(注意我仍然需要一些方法来提取适当的post_id值,并根据该Id创建表单),这根本不起作用。

// ==UserScript==
// @name           Quick_ReplyTest
// @namespace      http://userscripts.org/users/181447
// @description    Inserts QuickReply
// @include        *
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==


/* Optional:
window.addEventListener ("load", Greasemonkey_main, false);
*/

$(document).ready (Greasemonkey_main);


function Greasemonkey_main ()
{
    /*--- Get the first node inside the id="main" span (Google.com)
        If that's not there, then get the first node of the html body.
    */
    var TargetNode  = $("a[href*='event=reply/post']");
    if (!TargetNode)
        TargetNode  = $("body *:first");

    $(TargetNode).after
 (
  '<form method="POST" action="http://dl.tccd.edu/index.php/classforums/posts/event=saveReply">   \
  <input type="hidden" name="subject" size="45" id="txt_subject" maxlength="200" value="">        \
  <br> Message:<br>                                                                               \
  <textarea rows="20" style="width:70%;" name="message" id="message"></textarea>                  \
  <br> <br>                                                                                       \
  <input type="submit" id="submit_post" value="Post Reply">                                       \
  <input type="hidden" name="post_id" value="1010815">                                            \
  <input type="hidden" name="thread_id" value="1010815">                                          \
  </form>                                                                                         \
    '
 );
}

1 个答案:

答案 0 :(得分:0)

好的,这是修改后的脚本以使表格生效。它现在应该运行,但显然我无法在没有登录的情况下完全测试它。

我试图解释评论的内容,并且有一个方便的jQuery参考:http://www.jqapi.com/

/* NOTE:  Do not use:
    // @include        *
    This slows things down and could cause all sorts of interesting side-effects.
*/

// ==UserScript==
// @name            Quick_ReplyTest
// @namespace       http://userscripts.org/users/181447
// @description     Inserts QuickReply
// @include         http://*.tccd.edu/*
// @include         https://*.tccd.edu/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==


$(document).ready (Greasemonkey_main);


function Greasemonkey_main ()
{
    /*--- Use jQuery to get links (anchor tags) that point to the reply form.

        The links are of the form:
            <a href="http://dl.tccd.edu/index.php/classforums/posts/event=reply/post_id=1013402">Reply</a>
    */
    var zTargetNodes    = $("a[href*='event=reply/post']");     //-- href contains "event=reply/post".


    /*--- Now, for each link, extract the post_id and insert the reply form with the post_id filled in.

        Uses the jQuery each() function.
    */
    zTargetNodes. each (function (iPostNum) {AddReplyForm ($(this), iPostNum);} );
}


function AddReplyForm (zJnode, iPostNum)
{
    /*--- Extract the post_id from the link, using the jQuery attr() function and the javascript
        match() function.
    */
    var sHref       = zJnode.attr ('href');
    var sPostID     = (sHref + '&').match (/post_id=([^\&\#]+)[\&\#]/) [1];


    /*--- Build up the form HTML-string.  Using sPostID for post_id and thread_id.
        (Or we could just insert the form into the doc, and change the id values after.)
    */
    var sNewHTML    = '<form method="POST" action="http://dl.tccd.edu/index.php/classforums/posts/event=saveReply"> \n'
                    + '<input type="hidden" name="subject" size="45" id="txt_subject" maxlength="200" value=""> \n'
                    + '<br> Message:<br> \n'
                    + '<textarea rows="20" style="width:70%;" name="message" id="message"></textarea> \n'
                    + '<br> <br> \n'
                    + '<input type="submit" id="submit_post"   value="Post Reply"> \n'
                    + '<input type="hidden" name="post_id"     value="' + sPostID + '"> \n'
                    + '<input type="hidden" name="thread_id"   value="' + sPostID + '"> \n'
                    + '</form>'
                    ;


    /*--- Inject the reply form.
    */
    zJnode.after (sNewHTML);
}