简体   繁体   English

如何注入JS并从网站收集警报消息?

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

I am trying to collect alert from website by using overwrite method. 我正在尝试使用覆盖方法从网站收集alert I searched on google and found wappalyzer , a Chrome/Firefox extension to detect software on website. 我在Google上进行搜索,发现了wappalyzer ,这是一个Chrome / Firefox扩展程序,用于检测网站上的软件。 It injects a script inject.js when page load and collect information. 当页面加载并收集信息时,它将注入脚本inject.js My method is similar. 我的方法是相似的。 I make a local website and test it. 我制作了一个本地网站并对其进行了测试。 When I inject overwrite_alert.js manually then it works. 当我手动注入overwrite_alert.js ,它可以工作。

But I want to do it dynamically and apply to other website. 但我想动态地做,并应用于其他网站。 So I use headless browser like PhantomJS. 所以我使用无头浏览器,如PhantomJS。 The following code which I tried in PhantomJS but it does not work. 以下是我在PhantomJS中尝试过的代码,但它不起作用。

I am trying to inject a JavaScript on phantom JS. 我正在尝试在幻影JS上注入JavaScript。 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 : 来自这个问题的 overwrite_alert.js文件:

(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: 我尝试了onLoadStarted事件和My PhantomJS代码:

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 . 由于page.open回调是页面加载调用的,因此更改window.alert的实现很简单。 You would need to use earlier events such as page.onInitialized , page.onLoadStarted , etc. 您将需要使用早期事件,例如page.onInitializedpage.onLoadStarted等。

Since you're interested in alerts, you don't need to do that at all, because PhantomJS provides an event for that: page.onAlert 由于您对警报感兴趣,因此根本不需要这样做,因为PhantomJS为此提供了一个事件: page.onAlert

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当网站想要在计算机上使用其他存储时,如何显示警告消息 - How can I show an alert message when a website want to use additional storage on computer 如何将输入框(html 文件)中的值传递给 javascript 注入文件(inject.js)? - How can I pass a value from an input box (html file) to a javascript inject file (inject.js)? Discord.js:如何发送消息并从中收集反应 - Discord.js: How to send a message and collect reactions from it 如何在此 node.js 应用程序的警报中获取我的 err.message? - How can I get my err.message in the alert for this node.js app? 如何将 JS 注入已加载的 JS 文件中? - How can I inject JS into already loaded JS file? 如何在javascript警报消息启动时重复播放声音,并在警报消息解除时停止播放? - How can I repeatedly play a sound while a javascript alert message is up and stop it when the alert message is dismissed? 多语言网站中的警报消息 - alert message in Multilingual website 我如何将不同变量的多个不同值从 popup.js 发送到 inject.js 以便可以在 inject.js 脚本中使用? - How would I send multiple different values of different variables from popup.js to inject.js so those can be used in the inject.js script? 如何将 .NET 应用程序中的 JS 注入 Shopify 的动态主题? - How can I inject JS from my .NET application into Shopify's dynamic themes? 如何在面向对象的PHP中显示警报/消息? - How can I display an alert/ message in object-oriented PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM