2016-06-24 01:26:01 +12:00
|
|
|
|
2016-06-28 19:39:31 +12:00
|
|
|
import isNode from 'detect-node'
|
|
|
|
|
2016-08-23 19:19:01 +12:00
|
|
|
function fetchNode (url, responseType) {
|
2020-08-10 20:36:02 +12:00
|
|
|
const fetch = require('node-fetch').default
|
2018-03-13 10:06:35 +13:00
|
|
|
if (url.startsWith('/')) {
|
|
|
|
url = 'https://fimfiction.net' + url
|
2016-08-23 19:19:01 +12:00
|
|
|
}
|
2020-03-15 06:15:02 +13:00
|
|
|
return fetch(url, {
|
|
|
|
method: 'GET',
|
|
|
|
mode: 'cors',
|
|
|
|
credentials: 'include',
|
|
|
|
cache: 'default',
|
|
|
|
redirect: 'follow',
|
|
|
|
headers: {
|
|
|
|
cookie: 'view_mature=true',
|
|
|
|
referer: 'https://fimfiction.net/',
|
|
|
|
accept: 'Accept: text/*, image/png, image/jpeg' // Fix for not getting webp images from Fimfiction
|
|
|
|
}
|
|
|
|
}).then((response) => {
|
|
|
|
if (responseType) {
|
|
|
|
return response.buffer()
|
|
|
|
} else {
|
|
|
|
return response.text()
|
|
|
|
}
|
2016-06-28 19:39:31 +12:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:19:01 +12:00
|
|
|
export default function fetch (url, responseType) {
|
2018-03-13 10:06:35 +13:00
|
|
|
if (url.startsWith('//')) {
|
2016-06-24 01:26:01 +12:00
|
|
|
url = 'http:' + url
|
|
|
|
}
|
2018-03-13 10:06:35 +13:00
|
|
|
if (url.startsWith('/')) {
|
2017-06-08 02:17:40 +12:00
|
|
|
url = 'https://fimfiction.net' + url
|
|
|
|
}
|
2016-06-28 19:39:31 +12:00
|
|
|
|
|
|
|
if (isNode) {
|
2016-08-23 19:19:01 +12:00
|
|
|
return fetchNode(url, responseType)
|
2016-06-24 01:26:01 +12:00
|
|
|
}
|
2016-08-23 19:19:01 +12:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-11-22 01:52:49 +13:00
|
|
|
if (typeof window.fetch === 'function') {
|
|
|
|
window.fetch(url, {
|
|
|
|
method: 'GET',
|
|
|
|
mode: 'cors',
|
2017-06-07 08:15:05 +12:00
|
|
|
credentials: 'include',
|
2016-11-22 01:52:49 +13:00
|
|
|
cache: 'default',
|
2018-03-13 10:06:35 +13:00
|
|
|
redirect: 'follow',
|
|
|
|
headers: {
|
2020-03-15 06:15:02 +13:00
|
|
|
accept: 'Accept: text/*, image/png, image/jpeg' // Fix for not getting webp images from Fimfiction
|
2018-03-13 10:06:35 +13:00
|
|
|
},
|
2018-03-13 19:12:53 +13:00
|
|
|
referrer: window.location.origin
|
2016-11-22 01:52:49 +13:00
|
|
|
}).then((response) => {
|
|
|
|
if (responseType === 'blob') {
|
|
|
|
response.blob().then(resolve, reject)
|
|
|
|
} else if (responseType === 'arraybuffer') {
|
|
|
|
response.arrayBuffer().then(resolve, reject)
|
|
|
|
} else {
|
|
|
|
response.text().then(resolve, reject)
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
2017-10-19 01:59:30 +13:00
|
|
|
reject(new Error('Error fetching ' + url + ' (' + err + ')'))
|
2016-11-22 01:52:49 +13:00
|
|
|
})
|
|
|
|
} else {
|
2019-10-08 22:31:42 +13:00
|
|
|
const x = new XMLHttpRequest()
|
2018-03-13 10:06:35 +13:00
|
|
|
x.withCredentials = true
|
2020-03-15 06:15:02 +13:00
|
|
|
x.setRequestHeader('accept', 'text/*, image/png, image/jpeg') // Fix for not getting webp images from Fimfiction
|
2016-11-22 01:52:49 +13:00
|
|
|
x.open('get', url, true)
|
|
|
|
if (responseType) {
|
|
|
|
x.responseType = responseType
|
|
|
|
}
|
|
|
|
x.onload = function () {
|
|
|
|
resolve(x.response)
|
|
|
|
}
|
|
|
|
x.onerror = function () {
|
2017-10-19 01:59:30 +13:00
|
|
|
reject(new Error('Error fetching ' + url))
|
2016-11-22 01:52:49 +13:00
|
|
|
}
|
|
|
|
x.send()
|
2016-08-23 19:19:01 +12:00
|
|
|
}
|
|
|
|
})
|
2016-06-24 01:26:01 +12:00
|
|
|
}
|