Greasemonkey script to append text, to a form, whe

2019-07-20 04:33发布

问题:

So I am making a Greasemonkey script for a mybb forum. What it does is that when you submit a post it adds code to the beginning and the end of the post. Well even though that is a bad explanation just look at the code, it explains itself

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;

Now it works everywhere I want it to except the quickreply post reply button. I am assuming this is because the quickreply button uses AJAX to submit the form and the page does not get reloaded.

So I am wondering how I can have it so that when I click the quickreply button it appends the text I want it to. I have searched around for a while and anything that i could find did not work

Also, here is the code for the button that uses ajax(The button that doesn't work with the above code)

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

And here is where it is located

<!-- 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:

You need to show us the javascript that associates itself to that button. If it's AJAX powered, that's the only way to know what it's doing.

That said, this code will probably work:

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;


If it doesn't work, save the target page (complete HTML, JS, CSS) to a disk, zip the files together and share the zip -- so that we can see what is happening with that button.