且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-03 07:52:40

好,这是为了使表单生效而修改的脚本.它现在应该可以运行,但是很明显,如果没有登录,我无法完全测试它.

Ok, here is the script modified to make the form live. It should function now, but obviously I can't fully test it without a login.

我试图解释评论的内容,方便的jQuery参考位于: http://www .jqapi.com/.

I tried to explain what was going on on the comments and a handy jQuery reference is at: 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);
}