Skip to content

Instantly share code, notes, and snippets.

@JTRNS
Last active July 30, 2020 20:36
Show Gist options
  • Save JTRNS/0099bf1be1ea83a0a4f04c57e7b993f3 to your computer and use it in GitHub Desktop.
Save JTRNS/0099bf1be1ea83a0a4f04c57e7b993f3 to your computer and use it in GitHub Desktop.
Codebin | Saving code snippets that would otherwise have ended up in the bin
const fs = require('fs');
const path = require('path');
async function recurseDir (dirPath) {
const dir = await fs.promises.opendir(dirPath);
const allPaths = [];
for await (const dirent of dir) {
if (dirent.isDirectory()) {
const childPaths = await recurseDir(path.join(dirPath, dirent.name))
childPaths.map(p => allPaths.push(p))
} else {
allPaths.push(path.join(dirPath, dirent.name))
}
}
return allPaths
}
recurseDir('./').then(console.log)
function getAllTextIn (element) {
if (!element) element = document.body
const treeWalker =
document.createTreeWalker(
element,
NodeFilter.SHOW_TEXT,
{ acceptNode: (node) => {
return NodeFilter.FILTER_ACCEPT;
}},
false
);
const nodeList = [];
let currentNode = treeWalker.currentNode;
while(currentNode) {
nodeList.push(currentNode);
currentNode = treeWalker.nextNode();
}
return nodeList.slice(1).map(t => t.textContent).join('')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment