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