Last active
August 24, 2020 06:38
-
-
Save ver-1000000/d40e57a1c92f3fc4ae6ccbda40435ce4 to your computer and use it in GitHub Desktop.
RSSとかを取得するやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* {@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