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.
我试图通过使用覆盖方法从网站收集警报。我在google上搜索并发现了一个Chrome / Firefox扩展程序wappalyzer来检测网站上的软件。它在页面加载和收集信息时注入脚本inject.js。我的方法很相似。我建立了一个本地网站并进行测试。当我手动注入overwrite_alert.js然后它工作。
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.
但我想动态地做,并申请到其他网站。所以我使用像PhantomJS这样的无头浏览器。我在PhantomJS中尝试过以下代码,但它不起作用。
I am trying to inject a JavaScript on phantom JS. Like this code:
我试图在幻像JS上注入一个JavaScript。喜欢这段代码:
<!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
1 个解决方案
#1
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.
由于在加载页面后调用了page.open回调,因此更改window.alert的实现将会很晚。您需要使用之前的事件,例如page.onInitialized,page.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
#1
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.
由于在加载页面后调用了page.open回调,因此更改window.alert的实现将会很晚。您需要使用之前的事件,例如page.onInitialized,page.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