首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查作为响应提供的特定.js文件

检查作为响应提供的特定.js文件
EN

Stack Overflow用户
提问于 2015-02-11 03:47:40
回答 1查看 62关注 0票数 0

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

我需要自动检测js文件的缺失并通过电子邮件发送事件

我尝试了Net::Http,rest-client,mechanize和很多gem,它们只返回html头文件。似乎我需要使用像PhantomJS and checking for the file这样的工具来监控http流量,但是有没有什么this风格的方法可以做到呢?

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2015-03-11 23:56:14

我以phantomjs方法结束。ruby脚本遍历数据库表,然后为表示URL的每个记录调用此phantomjs脚本

这是phantomjs脚本

代码语言:javascript
复制
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);
		}
}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28440497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档