Skip to content

Instantly share code, notes, and snippets.

@xnuk
Created October 2, 2017 04:48
Show Gist options
  • Save xnuk/c6000a3cff5970a56d877f5607c7a4bc to your computer and use it in GitHub Desktop.
Save xnuk/c6000a3cff5970a56d877f5607c7a4bc to your computer and use it in GitHub Desktop.
// require https://raw.githubusercontent.com/mozilla/readability/master/Readability.js
function uri(x) {
const a = document.createElement('a')
a.href = x
const {href, hostname, pathname, protocol} = a
return {
spec: href, host: hostname,
prePath: protocol + '//' + hostname,
scheme: protocol.substr(0, protocol.indexOf(':')),
pathBase: protocol + '//' + hostname + pathname.substr(0, pathname.lastIndexOf('/') + 1)
}
}
async function doc(x) {
if(window.location.href === x) return new DOMParser().parseFromString(window.document.documentElement.outerHTML, 'text/html')
const response = await fetch(x)
const contentType = response.headers.get("content-type")
let ct = 'text/html'
if(!(
response.ok && contentType && (
contentType.includes("text/html") && ((ct = 'text/html'), true) ||
contentType.includes("application/xhtml") && ((ct = 'application/xhtml'), true) ||
contentType.includes("application/xml") && ((ct = 'application/xml'), true)
)
)) throw new TypeError("wat")
return new DOMParser().parseFromString(await response.text(), ct)
}
async function read(x) {
const d = await doc(x)
return new Readability(uri(x), d).parse()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment