我需要检测http响应中是否提供了特定的.js文件,此外,还需要检查它来自的域,如下所示:

我需要自动检测js文件的缺失并通过电子邮件发送事件
我尝试了Net::Http,rest-client,mechanize和很多gem,它们只返回html头文件。似乎我需要使用像PhantomJS and checking for the file这样的工具来监控http流量,但是有没有什么this风格的方法可以做到呢?
提前感谢
发布于 2015-03-11 23:56:14
我以phantomjs方法结束。ruby脚本遍历数据库表,然后为表示URL的每个记录调用此phantomjs脚本
这是phantomjs脚本
var page = require('webpage').create(),
system = require('system'),
address,
isScript = false;
var fs = require('fs');
// main
analizePage(system.args[1]);
//open page.
//onResourceRequested event, compares domain of each one with 'my.domain.net'
//append to a log file: -1 for failed url, 1 for script presence, 0 for no script presence
function analizePage(address){
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address ' + address);
fileWriter(-1, address);
}
else
{
if (!isScript){
fileWriter(0, address);
}
else
{
fileWriter(1, address);
}
console.log('Has script: ' + isScript);
}
phantom.exit(0);
});
page.onResourceRequested = function (req) {
try {
var link = document.createElement('a');
link.setAttribute('href', req.url); //extract asset's domain from URL
if (link.hostname == 'my.domain.net') {
isScript = true;
}
} catch(e) {
console.log("PAGE OPEN ERROR: " + e);
}
};
}
function fileWriter(type, line){
try {
fs.write("scriptlog.csv", type + ',' + line + ',' + Date.now() + ',' + system.args[2] + '\n', 'a');
} catch(e) {
console.log("FILE ERROR: " + e);
}
}
https://stackoverflow.com/questions/28440497
复制相似问题