views:

51

answers:

1

In a particular forum, clicking the reply button spawns a new window with a text form to type a reply. I want to implement a script to create that specific text form within the main page (instead of spawning a new window). How would I go about doing this?

Here is the source code for the pages I want to implement the script on:

http://pastebin.com/2UaUVGJA (the main discussion page) h~ttp://pastebin.com/hAx2SPUu (the reply page)

Here is the attempted script (note that I still need some method to extract the appropriate post_id value, and create the form based on that Id), that does not work at all.

// ==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"&gt;   \
  <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>                                                                                         \
    '
 );
}
A: 

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.

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"&gt;Reply&lt;/a&gt;
    */
    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"&gt; \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);
}
Brock Adams
Incredibly instructive! Thank you for taking the time to always answer my questions, and commenting the code. However, the script doesn't work. Is there anyway I can debug it to find the problem? Do I have to import the Jquery database, or does that "require" statement do it for me?
Yes, the "require" statement handles the import of jQuery. It does this only once, when the script is installed. So, if you think this has been corrupted or you made significant changes, use the Greasemonkey "Manage User Scripts" panel to uninstall and reinstall the script.
Brock Adams
*HOW* is it not working? It works great for me on a local copy of the page -- except that I can't post actual replies, of course. As for debugging... Yes, it's not too hard. Install the Firebug extension (http://getfirebug.com/). Then open Firebug's javascript console, turn on all error-tracking, and reload the page. Note any errors that were caused by the script (alas, most pages cause plenty of errors all by themselves). More Firebug-use info: http://stackoverflow.com/questions/511539/using-firebug-where-are-the-js-errors-shown/511563#511563
Brock Adams
Here's the error:$ is not defined$(document).ready (Greasemonkey_main); Line 16
Yeah, The script install got buggered somehow. Makes sure the "require" line is exactly as I specified. Uninstall the script and reinstall it. Also uninstall any other scripts with either (a) the same name, or (b) that run on the same page.
Brock Adams
I installed, uninstalled, and re-installed on two different computers and two different browsers. Same error each time. Is there any hackneyed way of solving the problem?Maybe putting each function inside: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> Would that work?
Use Firefox. What version of Greasemonkey do you have? And no, that <script> thing will not work.One other test: Go to the page in Firefox, Open the Firebug JavaScript console, temporarily turn-off Greasemonkey, and reload the page. Then enter `console.log ($);` into the command window and press "Run". What is the exact result? (one line).
Brock Adams
Greasemonkey version: 0.8.20100408.6. The error is thus:Error: console is not definedSource File: javascript:%20console.log%20($);Line: 1
When I typed the same code in in the address bar using javascript:console.log($); it says this in firebug: $ is not definedjavascript:console.log%20($);()
Jeez, multiple problems. Anywho, see: http://getfirebug.com/wiki/index.php/FAQ#I_get_an_error:_.22console_is_not_defined.22 and get that Firebug console working. I don't trust that address-bar result, but it suggests that the page should have no conflicts with jQuery.That's a good version of GM, but something is wrong with either your GM install or that copy of the script. Post your EXACT, unedited script.
Brock Adams
Got firebug working, after I type your console line it gives this response: anonymous()
Good. That means that most sources of possible conflicts with jQuery are not present. This leaves the GM install or edits made to the GM script.
Brock Adams
Go to the script's install-folder and see if jQuery is there.You should see: **jquerymin.js**On windows, the install folder will be something like:C:\Documents and Settings\ **Your-username** \Application Data\Mozilla\Firefox\Profiles\ **random-string** .default\gm_scripts\ **your-script-name**
Brock Adams
It wasn't in there, so I downloaded the Google AJAX API from here (http://docs.jquery.com/Downloading_jQuery) and put it in the directory. It still didn't work. Then I modified the @include line to just give the direct link to the local copy of Jquery saved on my hard drive, and it still didn't work. Hmmm, what next?Also, why do you have so much patience? You're awesome.
Did you download version 1.3.2? Only that version will work. The `@require` line needs to stay in place, too. A bad `@include` line might break the script.As for patience: 1) I kinda committed on your problem, so I should make a reasonable effort. 2) I like a challenge (to a point).
Brock Adams
Yeah, I downloaded the most recent version, and followed your instructions perfectly.
Argh! The most recent version does not work with Greasemonkey! **It MUST be version 1.3.2.**
Brock Adams