Skip to content

Instantly share code, notes, and snippets.

@justin-endler
Last active March 13, 2017 21:26
Show Gist options
  • Save justin-endler/dca2b8d316cc132d674d9d68a1f96923 to your computer and use it in GitHub Desktop.
Save justin-endler/dca2b8d316cc132d674d9d68a1f96923 to your computer and use it in GitHub Desktop.
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