├── .gitignore ├── .travis.yml ├── .editorconfig ├── test.js ├── package.json ├── license ├── readme.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '7' 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava' 2 | import waybackCss from './' 3 | 4 | test('returns css from a wayback url', async t => { 5 | t.plan(4) 6 | 7 | const results = await waybackCss('johnotander.com', '20151221') 8 | 9 | t.truthy(results.css) 10 | t.truthy(results.styles) 11 | t.truthy(results.links) 12 | t.truthy(results.inline) 13 | }) 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wayback-css", 3 | "description": "Get the css for a wayback machine url", 4 | "author": "John Otander", 5 | "version": "1.0.3", 6 | "main": "index.js", 7 | "files": [ 8 | "index.js" 9 | ], 10 | "scripts": { 11 | "test": "ava -v" 12 | }, 13 | "repository": "cssstats/wayback-css", 14 | "keywords": [], 15 | "license": "MIT", 16 | "dependencies": { 17 | "cheerio": "^0.22.0", 18 | "get-inline-styles": "^1.0.0", 19 | "got": "^6.6.3", 20 | "is-blank": "^1.1.0", 21 | "strip-html-comments": "^1.0.0", 22 | "strip-wayback-toolbar": "^1.0.0", 23 | "ua-string": "^1.0.0" 24 | }, 25 | "devDependencies": { 26 | "ava": "*" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 John Otander 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # wayback-css [![Build Status](https://secure.travis-ci.org/cssstats/wayback-css.svg?branch=master)](https://travis-ci.org/cssstats/wayback-css) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) 2 | 3 | Get the css for a wayback machine url. 4 | 5 | ## Installation 6 | 7 | ```bash 8 | npm install --save wayback-css 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```javascript 14 | const waybackCss = require('wayback-css') 15 | 16 | waybackCss('google.com', '20151221') // => YYYYMMDDhhss timestamp format 17 | .then(doStuff) 18 | .catch(handleError) 19 | ``` 20 | 21 | ## License 22 | 23 | MIT 24 | 25 | ## Contributing 26 | 27 | 1. Fork it 28 | 2. Create your feature branch (`git checkout -b my-new-feature`) 29 | 3. Commit your changes (`git commit -am 'Add some feature'`) 30 | 4. Push to the branch (`git push origin my-new-feature`) 31 | 5. Create new Pull Request 32 | 33 | Crafted with <3 by John Otander ([@4lpine](https://twitter.com/4lpine)). 34 | 35 | *** 36 | 37 | > This package was initially generated with [yeoman](http://yeoman.io) and the [p generator](https://github.com/johnotander/generator-p.git). 38 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const got = require('got') 3 | const cheerio = require('cheerio') 4 | const getInline = require('get-inline-styles') 5 | const stripComments = require('strip-html-comments') 6 | const stripWayback = require('strip-wayback-toolbar') 7 | 8 | module.exports = (url, timestamp) => { 9 | let waybackUrl = null 10 | 11 | return getAvailableUrl(url, timestamp) 12 | .then(url => waybackUrl = url && got(url)) 13 | .then(res => stripWayback(res.body)) 14 | .then(getCss) 15 | .then(getCssFromLinks) 16 | .then(aggregateCss) 17 | } 18 | 19 | const aggregateCss = css => { 20 | css.css = css.links.concat(css.styles).join(' ') 21 | 22 | return css 23 | } 24 | 25 | const normalizeLink = (baseUrl, link) => { 26 | if (/^http/.test(link)) { 27 | return link 28 | } else if (/^\/\//.test(link)) { 29 | return `http:${link}` 30 | } else { 31 | return `${baseUrl}${link}` 32 | } 33 | } 34 | 35 | const getCssFromLinks = css => { 36 | const baseUrl = 'http://web.archive.org' 37 | const linkCss = [] 38 | 39 | const px = css.links.map(link => { 40 | const loc = normalizeLink(baseUrl, link) 41 | 42 | return got(loc) 43 | .then(res => linkCss.push(res.body)) 44 | .catch(console.log) 45 | }) 46 | 47 | return Promise.all(px) 48 | .then(() => ({ 49 | styles: css.styles, 50 | inline: css.inline, 51 | links: linkCss 52 | })) 53 | } 54 | 55 | const getCss = html => { 56 | const $ = cheerio.load(html) 57 | 58 | const results = { 59 | html, 60 | links: [], 61 | styles: [], 62 | inline: getInline(html) 63 | } 64 | 65 | $('style').each(function () { 66 | results.styles.push(stripComments($(this).text())) 67 | }) 68 | 69 | $('link[rel=stylesheet]').each(function () { 70 | results.links.push($(this).attr('href')) 71 | }) 72 | 73 | return results 74 | } 75 | 76 | const getAvailableUrl = (url, timestamp) => { 77 | const availabilityUrl = `http://archive.org/wayback/available?url=${url}×tamp=${timestamp}` 78 | 79 | return got(availabilityUrl, { json: true }) 80 | .then(res => res.body.archived_snapshots.closest) 81 | .then(closest => closest.available && closest.url) 82 | } 83 | --------------------------------------------------------------------------------