![](/img/trans.png)
[英]How can I show an alert message when a website want to use additional storage on computer
[英]How can I inject JS and collect alert message from website?
我正在嘗試使用覆蓋方法從網站收集alert
。 我在Google上進行搜索,發現了wappalyzer ,這是一個Chrome / Firefox擴展程序,用於檢測網站上的軟件。 當頁面加載並收集信息時,它將注入腳本inject.js
。 我的方法是相似的。 我制作了一個本地網站並對其進行了測試。 當我手動注入overwrite_alert.js
,它可以工作。
但我想動態地做,並應用於其他網站。 所以我使用無頭瀏覽器,如PhantomJS。 以下是我在PhantomJS中嘗試過的代碼,但它不起作用。
我正在嘗試在幻影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
文件:
(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
};
})();
我嘗試了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");
}
});
結果:
$ phantomjs test_inject.js
CONSOLE> from onLoadStarted completed
null
CONSOLE> from page.open
null
由於page.open
回調是在頁面加載后調用的,因此更改window.alert
的實現很簡單。 您將需要使用早期事件,例如page.onInitialized
, page.onLoadStarted
等。
由於您對警報感興趣,因此根本不需要這樣做,因為PhantomJS為此提供了一個事件: page.onAlert
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.