I
I
ikerya2017-11-18 21:07:47
linux
ikerya, 2017-11-18 21:07:47

[PhantomJS] How can I assign a variable in the window object before the web page loads so that the variable can be called by the page's scripts?

I have been using this library for a long time, but then a problem arose. As it turned out, this library does not support video and audio (HTML5 tags). In principle, why are they needed on a browser emulator. But when trying to load a page that uses these tags, an error is returned (ReferenceError: Can't find variable: Audio), and further to the JS page cannot be executed because of it.
I tried to change the variable before page.open, but phantomjs still returned an error. My code:

var page = require("webpage").create();

page.open("http://95.183.11.171/ph.php", function(status) {
    page.evaluate(function() {
        if (!window.Audio) {
            window.Audio = function() {
                return {
                    play: function() {},
                    pause: function() {}
                };
            };
        }
    });

    setTimeout(function() {
        phantom.exit();
    }, 1000);
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ikerya, 2017-11-19
@ikerya

Found a solution for changing global page objects:
https://github.com/ariya/phantomjs/blob/master/exa...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question