Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Find css selectors in js file
'use strict';
const file = 'some_file.js';
const ignore = [
'a',
'input',
'span',
'td',
'title',
'img',
'div',
'body',
'dd',
'th',
'tr',
'br',
'script',
'p',
'li',
'selector',
'h4',
'b',
'ul',
'head',
'style',
'sub'
]; // etc
const fs = require('fs');
const lineReader = require('readline').createInterface({
input: fs.createReadStream(file)
});
var selectors = [];
lineReader.on('line', line => {
var match = line.match(/(?:\$|\.children|\.find|\.closest|\.next|\.prev)\s*?\(['"]([^\)]+)/g);
if (match) {
match.forEach(group => {
var selector = (group.match(/(?:\$|\.children|\.find|\.closest|\.next|\.prev)\s*?\(([^\)]+)/) || [])[1];
if (selector) {
selector = selector.replace(/["']/g, '').trim();
if (ignore.indexOf(selector) === -1 && selectors.indexOf(selector) === -1 && selector.indexOf('<') !== 0) {
console.log(selector);
selectors.push(selector);
}
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment