Skip to content

Instantly share code, notes, and snippets.

@rusco
Created April 9, 2018 11:35
Show Gist options
  • Save rusco/eebe2730d6afebe3e388a2cb814eded9 to your computer and use it in GitHub Desktop.
Save rusco/eebe2730d6afebe3e388a2cb814eded9 to your computer and use it in GitHub Desktop.
// 09.04.2018
// node --experimental-modules cssdownload.mjs
// downloads woff files embedded in links like https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons
import http from "http";
import readline from "readline";
import fs from "fs";
const rl = readline.createInterface({ input: fs.createReadStream("css.css"), crlfDelay: Infinity });
let idx = 0;
rl.on("line", (line) => {
if (line.indexOf("url(") > -1) {
const linkStart = line.indexOf("url(") + 4;
const linkStop = line.indexOf(")", linkStart);
const link = line.substring(linkStart, linkStop).replace("https", "http");
const fileNameStart = link.lastIndexOf("/") + 1;
const fileNameStop = link.lenght;
const fileName = link.substring(fileNameStart, fileNameStop);
const file = fs.createWriteStream(fileName);
http.get(link, (response) => response.pipe(file));
console.log(`${new String(idx++).padStart(2, "0")}: ${fileName} `);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment