Last active
July 30, 2020 20:36
-
-
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
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
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) |
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
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