0

My friend has an online webpage, which it has an inline script tag, there is a JavaScript function:

(#1):

var domain = window.location.protocol + "//" + window.location.host + '/';

$(document).ready(function() {
    var count = 5;
    countdown = setInterval(function() {
        if (count == 0) {
            $('#countdow').hide();
            $('#link-news').show()
        } else {
            $('#countdow').text(count);
            count--
        }
    }, 1700);
    $('#link-news').click(function() {
        var urls = $('input[name=linknexttop]').val();
        if (urls == 1) {
            $('input[name=linknexttop]').val(2);
            $.ajax({
                type: "GET",
                url: domain + "click.html",
                data: "code=Sh9QA&token=0982ff3066a3c60dbd3ecf9bcafc801b",
                contentType: "application/json; charset=utf-8",

                success: function(html) {
                    //alert(html);
                    window.location = html;
                }
            })
        }
    })
});

He has given a puzzle: "Can I run the below code without modified his original code?".

(#2):

$('input[name=linknexttop]').val(2);
$.ajax({
        type: "GET",
        url: domain + "click.html",
        data: "code=Sh9QA&token=0982ff3066a3c60dbd3ecf9bcafc801b",
        contentType: "application/json; charset=utf-8",

        success: function(html) {
            //alert(html);
            window.location = html;
        }

Rule for playing: "I am allowed to insert my code bellow his original code only; so, I am not allowed to change anything in his code. And, the most important, code=Sh9QA&token=0982ff3066a3c60dbd3ecf9bcafc801b is random.".


To save the time for waiting, I have used this extension to prevent the working of countdown();; and, add this code into it:

(#3):

$('#countdow').hide();
$('#link-news').show();

Next , I tried to go to pass the urls = $('input[name=linknexttop]').val();. I tried with urls == 1;; but, I failed.


My question is: "Is there any way to pass the urls = $('input[name=linknexttop]').val();, with all rules of this game are accepted?". If it is possible, please let me know.


royhowie
  • 11,075
  • 14
  • 50
  • 67
  • 3
    *"Can I run the below code without modified his original code?"* Sure, just add it to the page after it. Did you mean **instead of**? – T.J. Crowder Jul 31 '15 at 06:27
  • 3
    ***sigh*** Here's an idea: When someone asks for clarification, don't give them a hard time. They're trying to *help* you. ***Yes*** I read the question. The answer to the question is: Just add a script tag with that code. Therefore, I assumed there must be more to it. – T.J. Crowder Jul 31 '15 at 06:30
  • 1
    Is the code in an inline `script` tag, or one using `src`? If using `src`, is that coming from the same *origin* (in terms of the Same Origin Policy)? – T.J. Crowder Jul 31 '15 at 06:37
  • `var m = /"code=(\w+)&token=([0-9a-f]{32})"/.exec(document.getElementsByTagName('script')[/*number of that script tag*/].innerHTML); var code = m[1]; var token = m[2];` – Siguza Jul 31 '15 at 06:42

1 Answers1

1

If the script in question is in an inline script tag, you can find it by looping through the scripts and then extract the code from it, and use that in your inserted code.

Something along these lines, which you'd insert as a script element:

(function() {
    var code;
    $("script").each(function() {
        var match = /data=: "code=([^"]+)"/.exec($(this).text());
        if (match) {
            code = match[1];
            return false;
        }
    });
    $('#link-news').off("click");
    $('#link-news').click(function() {
        if (code) {
            $('input[name=linknexttop]').val(2);
            $.ajax({
                type: "GET",
                url: domain + "click.html",
                data: "code=" + code,
                contentType: "application/json; charset=utf-8",

                success: function(html) {
                    //alert(html);
                    window.location = html;
                }
            });
        }
    });
})();

In the above, I've assumed you're supposed to take over from the previous event handler, rather than adding to it. That's what the .off("click") does.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @d21p: I haven't played with that extension, no, but provided the other script is on the page, the above **or similar** should work when added. Tweaking may be required, although not much. This shows how to do what you've asked, though, in the normal web environment. – T.J. Crowder Jul 31 '15 at 06:59