omnivore/packages/api/src/utils/axios-handler.ts
Hongbo Wu e85273d87a
add support for axios newsletters (#49)
* add support for axios newsletters

* fix raw url

* fix getting newsletter url from axios

* add test for url

* add axios parser
2022-02-15 11:56:03 +08:00

36 lines
1 KiB
TypeScript

import { DOMWindow } from 'jsdom'
export class AxiosHandler {
name = 'axios'
shouldPrehandle = (url: URL, _dom: DOMWindow): boolean => {
const host = this.name + '.com'
// check if url ends with axios.com
return url.hostname.endsWith(host)
}
prehandle = (url: URL, dom: DOMWindow): Promise<DOMWindow> => {
const body = dom.document.querySelector('table')
// this removes ads and replaces table with a div
body?.querySelectorAll('table').forEach((el, k) => {
if (k > 0) {
el.remove()
} else {
// remove the last two rows of the table (they are ads)
el.querySelectorAll('tr').forEach((tr, i) => {
if (i >= el.querySelectorAll('tr').length - 2) {
console.log('removing', tr)
tr.remove()
}
})
// replace the table with a div
const div = dom.document.createElement('div')
div.innerHTML = el.innerHTML
el.parentNode?.replaceChild(div, el)
}
})
return Promise.resolve(dom)
}
}