views:

114

answers:

3

Alright, let me preface this by saying that I have little programming experience, so I apologize if my explanation belies some serious ignorance. I've always wanted to learn certain tricks but I can never find any tutorials.

Here's the deal:

There's a website that gives you various science questions and grades the input. For each question, there is a button called "Practice Other Versions" that produces a pop-up box with a new, similar version of the problem. This new version also has a button called "Show Answer", which, obviously, shows the solutions.

I used FireBug to find the code of the Show Answer button:

<input type="submit" onclick="key('0','1')" style="border: 1px solid rgb(51, 102, 153); margin-left: 1%;" value="Show Answer" name="Key">

I looked at the "DOM" tab of FireBug to look at the internal code of the key function:

function key(pos, showAnsFlag) {
    setYpos();
    if (showAnsFlag == 1) {
        document.forms[0].showAnswer.value = 1;
        document.forms[0].hideAnswer.value = 0;
    } else {
        document.forms[0].hideAnswer.value = 1;
        document.forms[0].showAnswer.value = 0;
    }
    document.forms[0].pos.value = "";
    document.forms[0].keypos.value = pos;
    document.forms[0].randpos.value = "";
    document.forms[0].solpos.value = "";
    document.forms[0].subaction.value = "key";
}

I also looked at the Net tab of Firebug to discover the POST parameters after I click "Show Solutions":

Key Show Answer
RC_821796_0_0_821813 mc  //[These are the names of the problems]
RC_821796_0_0_821813 1
RC_821796_0_1_821813 mc
RC_821796_0_1_821813 0
UserPass //[ERASED, because I'm not sure if it can be used maliciously]
feedback0 rmh
hideAnswer 0
hideSolution 
keypos 0
pos 
randpos 
showAnswer 1
showSolution 
solpos 
struct CuDMAcAACEAuEuEaANErASAJANBPBjDABkCiAaAxAEEfAjCgAvAjDjDQCTDTCbBjAUBtDXDXBqEXAFAjEPAVEMESvVBVzUCcEwALAMCSBQDewWDTAPBUEBCeArESCmDWAfErAXETBFEoBDCEDxBODlCbAeEnCNEKDmDgBHAJESEKATDmAoEFAdCUALCiCLBXCcDFATAcDRDoCwERuUBTDKEkBKESCXATAbDRAwBYEFAeDREWDBCdCeElBJCPCSDtAeABBxDwDDByEPAwEVAtAoDADWCkCCBBAwDNCyEECEAlCeACDCAPCmAsDM
subaction key
ypos 196

Alright, so all that's on the page when I "practice another version." My idea is that I want to call the "key" function (or at least a function that acts exactly like it), when I'm on the normal page, not the "Show Another Version" page. The problem is, when I check the DOM of the normal page, it doesn't even have a key function that is accessible.

Is there any way to inject javascript in the page to force a call to the key function? Optimally, it would be interesting to have a bookmarklet that looped through all of the Questions, calling the key function, and revealing the answers.

Given my paucity of knowledge, I didn't know what to try. I tried simple things like this (in the address bar)

  javascript:document.forms[0].showAnswer.value = 1;

And I know it did something, because when I type

  javascript:alert(document.forms[0].showAnswer.value);

It prompts "1", but I'm just not sure what exactly it's doing, or what else I have to do.

Edit

Let me try to clarify-- I don't own the site, so I'm trying to do some client-side javascript work to manipulate the site into doing what I want (mind you, I'm not even sure this is possible).

On one portion of the site, in a page called "practice.tpl" it allows me to press a button called "Show Answer" (the button calls the aforementioned key function and reveals the solution).

However, on another page, the button and the function do not exist. So I was wondering if I could somehow borrow the key function in the practice.tpl page, and inject it into another page.

Edit 2

This is the source code for the two pages:

Main page (the one where I'm trying to inject code) http://pastebin.com/r7KVMU1N

"Extra Problems" page (the one where the key function is) It won't let me post more than one link-- so de-obscure the following link: ht~tp://pastebin.co~m/D8Nc6fbk

I'd appreciate any help / references.

A: 

Don't have time for a detailed answer now, but you might only need a pointer. (I'll add more later, if needed.)

Anyway, it's not too difficult to use Greasemonkey to add "Show Answer" buttons to the main page and duplicate or even include the javascript from the "Practice Other Versions" page.

You can see a simple script that adds a button to a page here.

One important question: What is your browser? (I like to do Firefox-only code in my GM scripts, if I can get away with it. ;-) )

Brock Adams
I use Firefox and I have the greasemonkey add-on. I just don't know what I would have to program in order for it to work. Could you give me some references / guidance? What I would want to do is to iterate through all the forms on the main page, and somehow trigger a "showAnswer" property (which I don't believe that have on the normal page). Do you think there's anyway to do this?
Demosthenes
And I greatly appreciate your help. I've been trying to learn how to do this for the last month, and I decided I needed some counsel.
Demosthenes
A: 

On further inspection, this may not be possible, the key values seem to be locked into those struct parameters that the website passes everywhere.

Still, you can try this feasibility test. Firefox with Firebug is required.

  1. Log into a homework section.

  2. Right-click on the whitespace just to the left of the gray question border.

  3. A question-div, like <div id="question3" class="waQBox container"> should be selected. If not, select the nearest one in the HTML tab of Firebug.

  4. Press the Edit button.

  5. Copy and paste all of that code into any text editor with a search function.

  6. Search for "Submit New Answers To".

  7. The found line should contain text like: return submit_this('{BIG HONKING STRING}',true.
    Copy the value of the BIG HONKING STRING, you'll need it in a moment.

  8. Search for UserPass=. Copy the value between the = and the '.

  9. Go back to Firebug; scroll to the bottom of the edit box; and paste in the code, below, just above the third-from-last </div>.

  10. In the newly pasted block, replace the struct input's value with the BIG HONKING STRING from step 7.

  11. In the newly pasted block, replace the UserPass input's value with the string from step 8.

  12. Press the Edit button (edits are complete).

  13. Now, on the page, press the new Show Answer button.

  14. What happens? How the site responds determines whether a GM script is possible.

.

 <form enctype="x-www-form-urlencoded" action="/v4cgi/student/practice.tpl" name="practice" id="practice" method="post">
    <!-- ***** Javascript REQUIRED to copy current UserPass value.
    -->
    <input type="hidden" value="ceeb69f9b47e5660110d5ab7e27ba168" id="UserPass" name="UserPass">

    <input id="struct" name="struct" type="hidden" value="AYCJDoEaCWEbEVErBsBKAbBeDJEEDUBYDEDcDrEhCCBXAkEgBoExDGCDCxBODKBAAWAhAtDIAgCOAQAHACClAnCiCHCMBbEECLBICgBVALCHEhzUCxAxCrzZBPEHEiBSASDfCRCKCFBxDKCCBKCNvVCWBSBbAIAlDVCnBEEkAkBlBuCAEKCLDMDpAHBqCsCcEdAlBiDfCjErAUBTDmEoDqAcEVCfAyBPEJAeABBSEoEpDdEEwWDjCiCNAaESABCdEkDABKDoEuCvAOCRCLCFDfDeClBmAdAuAhELDeEoEnCNBoCOBgAFBfAr">
    <input type="hidden" value="key"    id="subaction" name="subaction">
    <input type="hidden" value=""       id="pos" name="pos">
    <input type="hidden" value="0"      id="keypos" name="keypos">
    <input type="hidden" value=""       id="randpos" name="randpos">
    <input type="hidden" value=""       id="solpos" name="solpos">
    <!-- ***** Javascript desired.  "7" to window.pageYOffset
    -->
    <input type="hidden" value="7"      id="ypos" name="ypos">
    <input type="hidden" value="1"      id="showAnswer" name="showAnswer">
    <input type="hidden" value="0"      id="hideAnswer" name="hideAnswer">
    <input type="hidden" value="" id="showSolution" name="showSolution">
    <input type="hidden" value="" id="hideSolution" name="hideSolution">
    <input type="hidden" value="r"      id="feedback0" name="feedback0">

    <!-- ***** If this value is required, then the whole process may be very difficult.
        But it's probably not.
    -->
    <input type="text" id="RN_821703_0_0_821745" name="RN_821703_0_0_821745" value="">

    <input type="submit" value="Show Answer" name="Key">
</form>
Brock Adams
A: 

Hey Brock,

This is Demosthenes-- I just got an account. I'm not sure if I'm placing your code in the right area. Here's what the bottom half of the code looks like after pasting and following your instructions:

http://pastebin.com/WSN6Kbrk

After clicking on "Show Answer" the website brought me to the front page and said that my SessionId has expired.

Even after filling in the correct RN_ id it still failed.

-Evan

The value of the `struct` pasted in, for step 10, looks suspiciously short. In fact it's right at 200 characters when a valid struct should be in the 300-char range. Maybe the editor you were using cut it short? Try again, and if the struct really is that short, and it still doesn't work, then it suggests that the site is state-tracking -- which makes the job of modding it much, much more difficult.
Brock Adams
Alright, so. I tried everything again, using Notepad++ and Chrome, and a different error came up. Rather then bringing me back to the home page, the site just brought me to a page that said, "There was a problem displaying the requested practice question", and Chrome threw the error "Uncaught Error: chrome.extension.getBackgroundPage is not supported in content scripts. See the content scripts documentation for more details."Any ideas? Even if this doesn't work out, I feel I'm learning a lot. Thanks so much for your patience.
`"There was a problem displaying the requested practice question"` Probably means one of 3 things: (1) There was a mismatch between the struct and maybe `RN_ id`. (2) The structs from the main page are unsuitable because one of their internal variables is different than what the practice page expects. (3) The site uses some kind of state tracking. Sooo. (A) Try again with an eye for possibility 1. (B) Repeat the 14 steps but do it from a "Practice" page. Use the password from the practice page but use a struct (and maybe RN_id) from the main page. You can paste the form in almost anywhere.
Brock Adams
Hey Brock,Sorry for not getting back to you. I'm new to this comment system, so I wasn't aware that you had responded. I followed every one of your instructions, but to no avail. When I used the struct from the practice page for your code, it just popped up the practice page with a new problem unrelated to the one on the main page.
However, I had an idea that I just tested and it worked. I took the RN_ id from the problem I wanted the answer to (from the main page) and I booted up the practice page. I clicked "Show Answer" on the Practice Problem, and I used TamperData to replace the RN_ value for the practice problem with the RN value from the main page. It worked! It booted up the correct answer to the main page. Now, the question is: can we do this programatically?
Yes, you can do it programmatically, but now it is much more difficult. Instead of just doing page mods, AJAX requests will be needed. It's a lot of work, and I can't help you more without access to the site (which I don't want). If it was me, I'd forget about this idea until I had more ajax under my belt.
Brock Adams