Skip to content

Instantly share code, notes, and snippets.

@ver-1000000
Last active August 24, 2020 06:38
Show Gist options
  • Save ver-1000000/d40e57a1c92f3fc4ae6ccbda40435ce4 to your computer and use it in GitHub Desktop.
Save ver-1000000/d40e57a1c92f3fc4ae6ccbda40435ce4 to your computer and use it in GitHub Desktop.
RSSとかを取得するやつ
/**
* {@param} url 取得したいDOMを返すURL
* {@param} mineType 取得したいDOMの[MINE TYPE]{@link https://developer.mozilla.org/ja/docs/Web/API/DOMParser#Argument02}
*
* url指定するとRSSフィードとかhtmlとかxmlとかが取得できるやつ。
*
* - 取得したいDOMと別ドメインで実行すると、CORSポリシーで弾かれるのでその辺注意
*/
const fetchDOM = async (url, mimeType = 'application/xml') => {
let body = '';
const decoder = new TextDecoder();
const read = ({ done, value }) => { body += decoder.decode(value); if (!done) { reader.read().then(read); } }
const reader = await fetch(url).then(res => res.body.getReader());
await reader.read().then(read);
return new DOMParser().parseFromString(body, mimeType);
};
await fetchDOM('https://gist.github.com/ver-1000000.atom');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment