简体   繁体   中英

How can I inject JS and collect alert message from website?

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

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

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