Created
April 18, 2015 00:16
-
-
Save johan/c14b1c3371d8acf934bc to your computer and use it in GitHub Desktop.
mixed-content warnings finder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env phantomjs | |
var webpage = require('webpage') | |
, system = require('system') | |
, fs = require('fs') | |
, _ = require('underscore') | |
, URLS, MULTIPLE, OPTS = {}, OPTIONS = | |
{ '-v': 'verbose' | |
}; | |
function loadIfFile(s) { | |
try { | |
var f = fs.read(s, 'utf-8'); | |
if (f) return f.split('\n').filter(function(i) { return i; }); | |
} catch(e) {} | |
return s; | |
} | |
function fail(status) { | |
console.log('Usage: scrape [-v] <urls>\n' + | |
'(urls may be urls or files with one url per line)'); | |
phantom.exit(status || 0); | |
} | |
function detectOptions(opt) { | |
var flag = OPTIONS[opt]; | |
if (flag) OPTS[flag] = true; | |
return !flag; | |
} | |
function debug() { | |
if (OPTS.verbose) console.log.apply(console, arguments); | |
} | |
if (system.args.length < 2) | |
fail(); | |
else { | |
var args = system.args.slice(1); | |
URLS = _.flatten(args.filter(detectOptions).map(loadIfFile)); | |
if (URLS.length) { | |
loadNext(); | |
} else | |
fail(1); | |
MULTIPLE = URLS.length; | |
} | |
function loadNext() { | |
url = URLS.shift(); | |
if (!url) phantom.exit(); | |
debug('Loading ' + url); | |
console.warn('url: ' + url); | |
load(url, loadNext); | |
} | |
insecure = /^The page at (.*?) displayed insecure content from (.*)\./g | |
function load(url, callback) { | |
function show(msg) { | |
if (show[msg]) return; | |
show[msg] = true; | |
if (!show.showedUrl) | |
console.log(url); | |
show.showedUrl = true; | |
console.log(' ' + msg); | |
} | |
var page = webpage.create(); | |
page.onConsoleMessage = function(msg) { | |
msg.split(/\n+/gm).forEach(function(line) { | |
if (mixed = insecure.exec(line)) { | |
show(mixed[2]); | |
} | |
}); | |
}; | |
page.open(url, function (status) { | |
if (status !== 'success') | |
console.log('Unable to load ' + url); | |
else { | |
debug('Loaded ' + url); | |
} | |
callback(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment