使用AJAX提交时,Greasemonkey脚本将文本附加到表单?

时间:2011-12-24 02:22:17

标签: javascript ajax forms submit greasemonkey

所以我正在为mybb论坛制作Greasemonkey脚本。它的作用是当你提交帖子时,它会在帖子的开头和结尾添加代码。好吧,即使这是一个糟糕的解释,只需看看代码,它解释了自己

function form_submit(event) {  
var form = event ? event.target : this;
   var arTextareas = form.getElementsByTagName('textarea');
   for (var i = arTextareas.length - 1; i >= 0; i--) {
       var elmTextarea = arTextareas[i];
       elmTextarea.value = "[font=Tahoma][color=white]" + elmTextarea.value + "[/color][/font]";
   }

   form._submit();
}

window.addEventListener('submit',form_submit, true);
HTMLFormElement.prototype._submit = HTMLFormElement.prototype.submit;
HTMLFormElement.prototype.submit = form_submit;

现在除了 quickreply 帖子回复按钮之外,它可以在我想要的任何地方使用。我假设这是因为 quickreply 按钮使用AJAX提交表单,并且页面不会重新加载。

所以我想知道我是如何拥有它的,所以当我点击 quickreply 按钮时,它会附加我想要的文本。我已经搜索了一会儿,我能找到的任何东西都不起作用

此外,这里是使用ajax的按钮的代码(该按钮不能与上面的代码一起使用)

<input id="quick_reply_submit" class="button" type="submit" accesskey="s" tabindex="2" value="Post Reply">

这就是它所在的位置

<!-- start: showthread_quickreply -->

<br />
<form method="post" action="newreply.php?tid=2023403&processed=1" name="quick_reply_form" id="quick_reply_form">
    <input type="hidden" name="my_post_key" value="de77ee8401edd4fe176f2c6a3787d411" />
    <input type="hidden" name="subject" value="*" />
    <input type="hidden" name="action" value="do_newreply" />
    <input type="hidden" name="posthash" value="a67ff7b68df0a0951770f7f4a24cce8f" id="posthash" />
    <input type="hidden" name="quoted_ids" value="" id="quoted_ids" />
    <input type="hidden" name="lastpid" id="lastpid" value="18370730" />
    <input type="hidden" name="from_page" value="1" />
    <input type="hidden" name="tid" value="2023403" />

    <input type="hidden" name="method" value="quickreply" />

    <table border="0" cellspacing="1" cellpadding="4" class="tborder">
        <thead>
            <tr>
                <td class="thead" colspan="2">
                    <div class="expcolimage"><img src="http://cdn.myforums.net/images/blackreign/collapse.gif" id="quickreply_img" class="expander" alt="[-]" title="[-]" /></div>
                    <div><strong>Quick Reply</strong></div>
                </td>

            </tr>
        </thead>
        <tbody style="" id="quickreply_e">
            <tr>
                <td class="trow1" valign="top" width="22%">
                    <strong>Message</strong><br />
                    <span class="smalltext">Type your reply to this message here.<br /><br />
                    <label><input type="checkbox" class="checkbox" name="postoptions[signature]" value="1" checked="checked" />&nbsp;<strong>Signature</strong></label><br />

                    <label><input type="checkbox" class="checkbox" name="postoptions[disablesmilies]" value="1" />&nbsp;<strong>Disable Smilies</strong></label></span>
                </td>
                <td class="trow1">
                    <div style="width: 95%">
                        <textarea style="width: 100%; padding: 4px; margin: 0;" rows="8" cols="80" name="message" id="message" tabindex="1"></textarea>
                    </div>
                    <div class="editor_control_bar" style="width: 95%; padding: 4px; margin-top: 3px; display: none;" id="quickreply_multiquote">
                        <span class="smalltext">

                            You have selected one or more posts to quote. <a href="./newreply.php?tid=2023403&load_all_quotes=1" onclick="return Thread.loadMultiQuoted();">Quote these posts now</a> or <a href="javascript:Thread.clearMultiQuoted();">deselect them</a>.
                        </span>
                    </div>
                </td>
            </tr>

            <tr>
                <td colspan="2" align="center" class="tfoot"><input type="submit" class="button" value="Post Reply" tabindex="2" accesskey="s" id="quick_reply_submit" /> <input type="submit" class="button" name="previewpost" value="Preview Post" tabindex="3" /></td>

            </tr>
        </tbody>
    </table>
</form>
<!-- end: showthread_quickreply -->

1 个答案:

答案 0 :(得分:0)

您需要向我们展示将自己与该按钮相关联的JavaScript。如果它是AJAX驱动的,这是了解它正在做什么的唯一方法。

那就是说,这段代码可能工作:

function form_submit (event) {

    var form, bClickNotSubmit;

    if (event  &&  event.type == 'click') {
        bClickNotSubmit = true;
        form            = document.getElementById ('quick_reply_form');
    }
    else {
        bClickNotSubmit = false;
        form            = event ? event.target : this;
    }

    var arTextareas = form.getElementsByTagName ('textarea');

    for (var i = arTextareas.length - 1; i >= 0; i--) {
        var elmTextarea     = arTextareas[i];
        elmTextarea.value   = "[font=Tahoma][color=white]" + elmTextarea.value + "[/color][/font]";
    }

    if ( ! bClickNotSubmit ) {
        form._submit();
    }
}

window.addEventListener ('submit', form_submit, true);
document.getElementById ('quick_reply_submit').addEventListener ('click', form_submit, true);

HTMLFormElement.prototype._submit = HTMLFormElement.prototype.submit;
HTMLFormElement.prototype.submit = form_submit;


如果它不起作用,将目标页面(完整的HTML,JS,CSS)保存到磁盘,将文件压缩在一起并共享zip - 这样我们就可以看到该按钮发生了什么。