简体   繁体   中英

How can I return a value from inside of multiple functions?

Here is the code I'm working with:

(function() {
    var origOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function() {
        this.addEventListener('load', function() 
            console.log(this.responseText); //return this value
        });
        origOpen.apply(this, arguments);
    };
})();

I'm using selenium's execute_script() to execute the above code on to the website. How can I return this.responseText to a variable?

Thank you.

this.responseText obtain value when XMLHttpRequest responds, asynchronously.

If you need execute code when XMLHttpRequest finish you must pass as a callback, or call directly in the event; inside that call you can redirect the application flux.

Example, directly (I try not to change your original code):

function oCallback(responseText){
    console.log('continues execution');
    console.log(responseText);
}

(function() {
    var origOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function() {
        this.addEventListener('load', function() 
            //console.log(this.responseText); //return this value
            oCallback(this.responseText);
        });
        origOpen.apply(this, arguments);
    };
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM