0

This script is not from my website, I would like to call crocodile() because now I have to wait 20 seconds before it is called.

I would like to achieve it via browser console or UserJS or Selenium.

However I suspect it is not possible as explained in:

Access and modify variable in anonymous function as parameter in other function JS/Angular

Is it right? I am not sure, because I have little experience with js

$.fn.countdown = function (callback, duration, message) {
    var countdown = setInterval(
    ....
    callback.call(...);  
    ...

    , 1000);
};

$(document).ready(function () {

    var link = 'https://example.com/c?p=1';
    var userId = parseInt(2);

    if (isNaN(userId)) {
        ....
    } else {
        /* counts down 20 seconds before call */
        $(".countdown").countdown(crocodile, 20, "");  
    }


    function crocodile() {

        var _l = 'https://example.com/c?p=1';
        var _sID = parseInt(1);
        var _uID = parseInt(2);

        $.ajax({
            type: "GET",
            cashe: false,
            url: '/example',
            data: {
                idS: _sID,
                userId: _uID,
                link: _l
            },
            dataType: "json",
            traditional: true,
            success: function (data) {
                $(location).prop('href', data.url);
            }
        });
    }
});
Karol Zlot
  • 2,887
  • 2
  • 20
  • 37
  • You cannot modify any of the code in the question, right? You can only work around it? I have an idea, is it possible for you to run code *after* `$` gets defined, but *before* the `$(document).ready` runs? – CertainPerformance Dec 01 '18 at 04:06
  • 1
    You can just copy / paste the function into the chrome console, for instance. – pguardiario Dec 01 '18 at 04:48
  • @CertainPerformance Yes, I cannot modify code. I am not sure if I can, but I can use any javascript method I think – Karol Zlot Dec 01 '18 at 05:07
  • @pguardiario Oh, although this is not perfect solution, that works. I think that it may be possible to do similar thing in selenium. Thank you – Karol Zlot Dec 01 '18 at 05:13
  • 1
    Yes in selenium you would just add it with executeScript. You might want to add it to window. In other words, `window.crocodile = () => {}` – pguardiario Dec 01 '18 at 07:14

1 Answers1

1

The crocodile function is defined inside the document ready function. That means it can only be called from within that function, since it does not exist outside that scope. You’ll have to move the method definition out of the document ready call, if you want to be able to call it externally using your test suite as opposed to waiting 20 seconds (or at least that’s what the countdown comment says) for it to fire.

If you’re looking to test this without waiting so long, consider making that 20 seconds configurable, so that your test suite can shorten it. Additionally if you define the crocodile function outside the document ready call, you can actually unit test that function.

Nate
  • 2,364
  • 1
  • 10
  • 16