0

I am trying to collect alert from website by using overwrite method. I searched on google and found wappalyzer, a Chrome/Firefox extension to detect software on website. It injects a script inject.js when page load and collect information. My method is similar. I make a local website and test it. When I inject overwrite_alert.js manually then it works.

But I want to do it dynamically and apply to other website. So I use headless browser like PhantomJS. The following code which I tried in PhantomJS but it does not work.

I am trying to inject a JavaScript on phantom JS. Like this code:

<!DOCTYPE html>
<html>
<head>
    <title>Test alert</title>

    <!-- !!! Inject here !!! <script src="overwrite_alert.js"></script> -->

    <script type="text/javascript" src="other_script.js"> </script>
    <script type="text/javascript">
        alert("Alert content");
    </script>
</head>
<body>
<h1>website content</h1>
</body>
</html>

overwrite_alert.js file from this question:

(function() {
    var _alert = window.alert;                   // <-- Reference
    window.alert = function(str) {
        // do something additional
        if(console) console.log(str);
        //return _alert.apply(this, arguments);  // <-- The universal method
        _alert(str);                             // Suits for this case
    };
})();

I tried with onLoadStarted event and My PhantomJS code:

var webPage = require('webpage');
var page = webPage.create();

var url = "https://localhost:5000/alert.html";


page.onConsoleMessage = function(msg, lineNum, sourceId) {
    console.log('CONSOLE> ' + msg);
};

page.onLoadStarted = function() {
    if (page.injectJs('do.js')) {
        var title = page.evaluate(function() {
            // returnTitle is a function loaded from our do.js file - see below
            console.log("evaluate completed");
        });
        console.log(title);
    }
}


page.open(url, function(status) {
    if (status === "success") {
        if (page.injectJs('do.js')) {
            var title = page.evaluate(function() {
                // returnTitle is a function loaded from our do.js file - see below
                console.log("evaluate completed");
            });
            console.log(title);
            phantom.exit();
        }


        page.render("onOpen.png");
    }
});

Result:

$ phantomjs test_inject.js
CONSOLE> from onLoadStarted completed
null
CONSOLE> from page.open
null
Community
  • 1
  • 1
kha
  • 349
  • 3
  • 18

1 Answers1

0

Since the page.open callback is called after a page is loaded, it would be simply to late to change the implementation of window.alert. You would need to use earlier events such as page.onInitialized, page.onLoadStarted, etc.

Since you're interested in alerts, you don't need to do that at all, because PhantomJS provides an event for that: page.onAlert

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • `page.onInitialized` and `page.onLoadStarted` do not work. I want to do overwrite method because in the future, I will implement not only `alert` function but also another function. – kha May 26 '16 at 05:21
  • I can't reproduce your issue. It works for me: https://gist.github.com/artjomb/3867677bf4a9a97966879d49e6594943 – Artjom B. May 27 '16 at 16:40