Skip to content

Instantly share code, notes, and snippets.

@danhper
Created August 27, 2015 07:22
Show Gist options
  • Save danhper/237b8766afa357843b8a to your computer and use it in GitHub Desktop.
Save danhper/237b8766afa357843b8a to your computer and use it in GitHub Desktop.
Check what tag is used for `main`
'use strict';
var async = require('async');
var _ = require('lodash');
var cheerio = require('cheerio');
var phantom = require('phantom');
var userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1';
var urls = [
'https://www.google.com',
'https://www.apple.com',
'http://www.amazon.com',
'https://twitter.com',
'https://github.com',
'https://www.facebook.com',
'https://www.yahoo.com',
'https://www.pinterest.com'
];
var mainSelectors = [
'main',
'#main',
'.main',
'[role="main"]'
];
function checkMainUsage(url, body) {
var $ = cheerio.load(body);
var mainFound = false;
_.forEach(mainSelectors, function (selector) {
if ($(selector).length > 0) {
console.log(url + ' is using ' + selector);
mainFound = true;
}
});
if (!mainFound) {
console.log(url + ' does not seem to use any main like tag');
}
}
function runPhantom(ph, url, done) {
async.waterfall([
function (cb) {
ph.createPage(function (page) { cb(null, page); });
},
function (page, cb) {
page.set('settings.userAgent', userAgent);
page.open(url, function () { cb(null, page); });
},
function (page, cb) {
page.evaluate(function () {
return document.querySelector('body').innerHTML;
}, function (body) {
checkMainUsage(url, body);
cb();
});
}
], done);
}
phantom.create(function (ph) {
async.each(urls, function (url, cb) {
runPhantom(ph, url, cb);
}, function () {
ph.exit();
});
});
// result
// https://www.google.com is using #main
// https://github.com is using [role="main"]
// https://twitter.com does not seem to use any main like tag
// https://www.yahoo.com is using [role="main"]
// https://www.facebook.com is using [role="main"]
// https://www.apple.com does not seem to use any main like tag
// https://www.pinterest.com does not seem to use any main like tag
// http://www.amazon.com does not seem to use any main like tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment