track javascipt function call in a webpage using phantomjs

75 views
Skip to first unread message

Ryan Sun

unread,
Oct 19, 2016, 6:59:06 PM10/19/16
to phantomjs
Hi all,

I want to check which function get called and how many times it get called when loading a webpage. Is it possible to do that with phantomjs?
For example, eval() and document.write() function.

Thanks.

Ryan Sun

unread,
Oct 19, 2016, 7:09:02 PM10/19/16
to phantomjs
BTW I don't have any control over the webpage, so no code can be added to it.

Jules C

unread,
Oct 20, 2016, 4:56:53 PM10/20/16
to phantomjs
On Wednesday, October 19, 2016 at 4:09:02 PM UTC-7, Ryan Sun wrote:
BTW I don't have any control over the webpage, so no code can be added to it.

actually you can totally make modifications to the page and inject code to count function calls and such:


~j
Message has been deleted

Jules

unread,
Nov 2, 2016, 9:33:46 PM11/2/16
to phan...@googlegroups.com

I think the problem is that document.write needs context.

try oldDocumentWrite.call(document, arts);


On Nov 2, 2016 15:43, "Ryan Sun" <lish...@gmail.com> wrote:
Came up with the following code to override document.write() function. Log can be printed but the part calling original function is not working (marked by <---). Any thought?
----------------------------------------------------------------------------------
var page = require('webpage').create(),
    system = require('system'),
    url;

url = system.args[1];
page.onConsoleMessage = function(msg) {
    console.log(msg);
};

page.onInitialized = function() {
    page.evaluate(function() {
        var oldDocumentWrite = document.write;
        document.write = function(arg) {
            console.log('document.write() called: '+arg);
            return oldDocumentWrite(arg);  <------ NOT WORKING
        };
    });
};

page.open(url, function (status) {
    if (status !== 'success') {
        console.log('FAIL to load the address');
    }

    console.log(page.content);
    phantom.exit();
});
----------------------------------------------------------------------------------

--
You received this message because you are subscribed to a topic in the Google Groups "phantomjs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/phantomjs/6Z3yYG46Lk8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to phantomjs+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/phantomjs.
For more options, visit https://groups.google.com/d/optout.

Ryan Sun

unread,
Nov 7, 2016, 1:02:06 PM11/7/16
to phantomjs
Thank you, your solution works. Another solution I found also works:

        document.base_document_write = document.write;
        document.write = function(arg) {
            console.log('document.write() called:\n'+arg);
            document.base_document_write(arg);
        };
To unsubscribe from this group and all its topics, send an email to phantomjs+...@googlegroups.com.

Ryan Sun

unread,
Nov 10, 2016, 2:48:00 PM11/10/16
to phantomjs
Another question I have is the code works for the main page, but if there is an <iframe>, the javascript calls in this iframe wont be tracked. Ex:

main_page.html:
<html><body><script> SCRIPT_1 </script><iframe src="frame.html"></iframe></body></html>

frame.html:
<html><body><script> SCRIPT_2 </script></body></html>

All the function calls in SCRIPT_1 will be tracked, while not for SCRIPT_2, or the console.log() print out from the iframe couldn't be received by the main page while phantomjs evaluating SCRIPT_2?

Thanks for helping!

Ryan Sun

unread,
Nov 21, 2016, 5:29:46 PM11/21/16
to phantomjs
Found a work-around: switch to the frame, then assign the frame's content back to main page, which will trigger reloading. But since the frame content is not the original (js rendered instead), functions may be counted multiple times.
Reply all
Reply to author
Forward
0 new messages