【问题标题】:Check for an specific .js file being served in response检查响应中提供的特定 .js 文件
【发布时间】:2015-02-10 19:47:39
【问题描述】:

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

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

我尝试了 Net::Http、rest-client、mechanize 和很多 gem,它们只返回 html 标头。看来我需要使用PhantomJS and checking for the file 之类的工具来监控 http 流量,但是有没有 ruby​​esque 的方法可以做到这一点?

提前致谢

【问题讨论】:

    标签: javascript ruby network-programming monitoring


    【解决方案1】:

    我以 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);
    		}
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-21
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 2018-02-13
      • 2018-05-12
      相关资源
      最近更新 更多