Check for an specific .js file being served in response - javascript

I need to detect whether an specific .js file was served in a http response and additionally, check the domain it came from, like this:
I need to automatically detect the lack of the js file and email the incidence
I tried Net::Http, rest-client, mechanize and a lot of gems, they just return the html header. It seems I need to monitor http traffic with tools like PhantomJS and checking for the file, but is there any rubyesque way of doing this?
Thanks in advance

I ended with the phantomjs approach. A ruby script iterate over a database table and then calls this phantomjs script for each record representing an URL
This is the phantomjs script
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);
}
}

Related

Set HTML in Outlook Cached Mode via Javascript and EWS

My work Outlook environment is changing from online (continuous connection) to cached mode and that breaks the desktop component of an Outlook add-in designed to save and then forward draft emails to an in-house security check.
My original code would save the draft async, then use the Id and change key to then forward it. In cached mode, there is no Id until the account synchs with the Exchange.
Now broken code (does not work in cached mode)
//Save current item as draft
mailForward.SaveDraftIfNecessary = function SaveDraftIfNecessary() {
if (_item_id == null || _item_id == undefined) {
Office.context.mailbox.item.saveAsync(function (asyncResult) {
if (asyncResult.error) {
app.showNotification(_appName, "Error: " + asyncResult.error.message);
}
else {
_item_id = asyncResult.value;
}
});
}
};
I've been able to write code that can replicate the mail and send it, but I need to be able to insert the draft body into the new email body, but it does not accept it. It does not work for text or HTML.
The code for getting the body type and text is as follows:
item.body.getTypeAsync(
function (asyncResultGet) {
if (asyncResultGet.status === Office.AsyncResultStatus.Failed) {
statusUpdate("icon16", asyncResultGet.error.message);
stopSpinner();
}
else {
bodyType = asyncResultGet.value;
item.body.getAsync(bodyType,
function (asyncResultGet) {
if (asyncResultGet.status === Office.AsyncResultStatus.Failed) {
statusUpdate("icon16", asyncResultGet.error.message);
stopSpinner();
}
else {
bodyText = asyncResultGet.value;
The SOAP that I am trying to use to insert the body text. I have also tried t:NewBodyContent instead of t:Body:
' <t:Message>' +
' <t:Subject>' + _subject + '</t:Subject>' +
' <t:Body BodyType="'+ _bodyType +'">' + _bodyText + '</t:Body>' +
' <t:ToRecipients>' + _adddressesSoap + '</t:ToRecipients>' +
' </t:Message>' +
My issue was encoding. I had an inkling that might be the problem, but didn't test it out until I found this link:
HTML-encoding lost when attribute read from input field
The code I used is this:
var htmlEncode = function htmlEncode(s) {
var ntable = {
"&": "amp",
"<": "lt",
">": "gt",
"\"": "quot"
};
s = s.replace(/[&<>"]/g, function (ch) {
return "&" + ntable[ch] + ";";
});
s = s.replace(/[^ -\x7e]/g, function (ch) {
return "&#" + ch.charCodeAt(0).toString() + ";";
});
return s;
};

Phantomjs can't load network traffic when key down event sent

I tried to load the network traffic from a twitter page using Phantomjs, the problem is the keypress event didn't worked. here the code (netlog.js modified)
in other words I didn't see any output with sentEvent() unlike wothout sentEvent(), i want to scroll down the twitter page to get more network traffic.
var page = require('webpage').create(),
system = require('system'),
address;
if (system.args.length === 1) {
console.log('Usage: netlog.js <some URL>');
phantom.exit(1);
} else {
address = system.args[1];
page.onResourceRequested = function (req) {
console.log('requested: ' + JSON.stringify(req, undefined, 4));
};
page.onResourceReceived = function (res) {
console.log('received: ' + JSON.stringify(res, undefined, 4));
};
page.onLoadFinished = function(status) {
console.log('Status: ' + status);
};
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
}else{
page.sendEvent('keypress', page.event.key.Down);
}
phantom.exit();
});
}
Thanks for your Help.

How to save the JS output to a file

I have the following JS that is run using phantomjs.
var webPage = require('webpage');
var system = require('system');
var page = webPage.create();
page.customHeaders = {
"pragma": "akamai-x-feo-trace"
};
if (system.args.length === 1) {
console.log('Try to pass some args when invoking this script!');
} else {
page.open(system.args[1], function (status) {
var content = page.content;
console.log('Content: ' + content);
phantom.exit();
});
}
I wanto to save the output of console.log('Content: ' + content); to a folder with ./html/<random#>.html.
How should I go about it?
It seems that you only want to save the page content. You can use the Filesystem module to write the content to a file. A random filename can be generated by invoking ''+(Math.random()*100000000000000000)+'.html'.

extjs 4.1 download xml file using ajax

i want to download a file using extjs 4.1.
The file name is "wsnDataModel.xml".
I've tryed with all things suggested in other posts:
//function invoked clicking a button
DwDataModel : function(th, h, items) {
//direct method that build in file in the location calculated below with "certurl" (I've verified)
Utility.GetDataModel(function(e, z, x) {
if (z.message) {
//the server method should give an error message
Ext.create('AM.view.notification.toast', {
title : 'Error',
html : z.message,
isError : true
}).show();
} else {
// navigate to get data
var certurl = 'http://' + window.location.host
+ '/AdminConsole3/' + e;
Ext.Ajax.request({
method : 'GET',
url : 'http://' + window.location.host
+ '/AdminConsole3/' + e,
success : function(response, opts) {
//the following navigate and openthe file in the current browser page.
//I don't want to change the current browser page
//window.location.href = certurl;
//the same behaviour with
//document.location = certurl;
//and this don't work at all
window.open(certurl,'download');
},
failure : function(response, opts) {
console
.log('server-side failure with status code '
+ response.status);
console.log('tried to fetch ' + url);
}
}, this, [certurl]);
}
}, th);
}
the "navigation" redirect the application (i don't want to redirect the application) like this:
and I'would like to download the file like this image:
I think it's very simple. How to do that?
thank you
Very simple: this is the success function.
success: function (response, opts) {
var link = document.createElement("a");
//this gives the name "wsnDataModel.xml"
var fileName = certurl.substring(certurl.lastIndexOf('/') + 1);
link.download = fileName;
link.href = certurl;
link.click();
}

PhantomJS doesn't send authentication header

I'm trying to open a web page which requires HTTP authentication, in PhantomJS.
My script is based off the loadspeed.js example:
var page = require('webpage').create(),
t, address;
page.settings.userName = "user";
page.settings.password = "password";
if (phantom.args.length === 0) {
console.log('Usage: loadspeed.js <some URL>');
phantom.exit();
} else {
t = Date.now();
address = phantom.args[0];
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
t = Date.now() - t;
console.log('Loading time ' + t + ' msec');
page.render('page.jpg');
}
phantom.exit();
});
}
I can see from the rendered page.jpg that I'm getting a 401 every time.
I've also traced the HTTP session using Wireshark, which reveals that no authentication header is sent in the GET request to the given URL.
What am I doing wrong here? I'm just getting started with PhantomJS but I've been searching all evening and not gotten far...
PhantomJS (at least as of 1.9.0) has a bug with auth: it sends the request without the auth headers, and then only after it gets the 401 back does it do the request again but this time with the headers. (That is for GET; with POST it doesn't work at all.)
The workaround is simple, so instead of:
page.settings.userName = 'username';
page.settings.password = 'password';
you can use:
page.customHeaders={'Authorization': 'Basic '+btoa('username:password')};
(I just covered this in a blog post: http://darrendev.blogspot.jp/2013/04/phantomjs-post-auth-and-timeouts.html, and learnt that workaround on the PhantomJS mailing list from Igor Semenko.)
I dont think there is anything wrong with the script your using or phantomjs (at least in v1.5).
If you try this script:
var page = require('webpage').create(),
system = require('system'),
t, address;
page.settings.userName = 'test';
page.settings.password = 'test';
if (system.args.length === 1) {
console.log('Usage: loadspeed.js <some URL>');
phantom.exit();
} else {
t = Date.now();
address = system.args[1];
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
t = Date.now() - t;
console.log('Page title is ' + page.evaluate(function () {
return document.title;
}));
console.log('Loading time ' + t + ' msec');
}
phantom.exit();
});
}
phantomjs loadspeed.js http://browserspy.dk/password-ok.php
The auth is successful.

Categories