1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2024-11-22 16:17:29 +00:00

Update userscript to use greasemonkey fetch API

This commit is contained in:
Cadence Ember 2020-06-19 20:00:46 +12:00
parent 0c16c027e9
commit 719399f54b
No known key found for this signature in database
GPG Key ID: 128B99B1B74A6412

View File

@ -4,8 +4,8 @@
// <instance_match_list> // <instance_match_list>
// @downloadURL <website_origin>/userscripts/unblock.user.js // @downloadURL <website_origin>/userscripts/unblock.user.js
// @updateURL <website_origin>/userscripts/unblock.user.js // @updateURL <website_origin>/userscripts/unblock.user.js
// @grant none // @grant GM.xmlHttpRequest
// @version 1.0 // @version 1.1
// @author cloudrac3r // @author cloudrac3r
// ==/UserScript== // ==/UserScript==
@ -35,6 +35,34 @@ function applyStyles(element, styles) {
} }
} }
class GMResponse {
constructor(data) {
this.headers = data.responseHeaders
this.text = data.responseText
this.status = data.status
}
toJSON() {
return JSON.parse(this.text)
}
}
function gmFetch(url, options = {}) {
console.log("Making request")
return new Promise((resolve, reject) => {
options.url = url
if (!options.method) options.method = "GET"
options.onload = response => {
console.log("Got response", response)
resolve(new GMResponse(response))
}
options.onerror = response => {
reject(response)
}
GM.xmlHttpRequest(options)
})
}
if (q("#bibliogram-identifier-blocked")) { if (q("#bibliogram-identifier-blocked")) {
const scriptStatus = addChild(q("#dynamic-status-area"), "p", "Unblocker script is processing...", "explanation") const scriptStatus = addChild(q("#dynamic-status-area"), "p", "Unblocker script is processing...", "explanation")
applyStyles(scriptStatus, {border: "solid orange", borderWidth: "1px 0px", padding: "10px", marginTop: "20px", textAlign: "center"}) applyStyles(scriptStatus, {border: "solid orange", borderWidth: "1px 0px", padding: "10px", marginTop: "20px", textAlign: "center"})
@ -47,10 +75,9 @@ if (q("#bibliogram-identifier-blocked")) {
const username = q("#data").getAttribute("data-username") const username = q("#data").getAttribute("data-username")
fetch(`https://www.instagram.com/${username}/`).then(res => { gmFetch(`https://www.instagram.com/${username}/`).then(res => {
if (res.status === 200) { if (res.status === 200) {
res.text().then(text => { const text = res.text
const id = (text.match(/"id":"([0-9]+)"/) || [])[1] const id = (text.match(/"id":"([0-9]+)"/) || [])[1]
if (id) { if (id) {
@ -58,21 +85,20 @@ if (q("#bibliogram-identifier-blocked")) {
params.append("username", username) params.append("username", username)
params.append("user_id", id) params.append("user_id", id)
fetch(`${window.location.origin}/api/suggest_user/v1`, { gmFetch(`${window.location.origin}/api/suggest_user/v1`, {
headers: { headers: {
"content-type": "application/x-www-form-urlencoded" "content-type": "application/x-www-form-urlencoded"
}, },
method: "POST", method: "POST",
body: params.toString() data: params.toString()
}).then(res => { }).then(res => {
if (res.status === 201) { if (res.status === 201) {
scriptStatus.textContent = "Done! Please wait to be redirected..." scriptStatus.textContent = "Done! Please wait to be redirected..."
flashBackground(); flashBackground()
} else { } else {
res.json().then(data => { const data = res.toJSON()
console.log(data) scriptStatus.textContent = data.message
alert(data.message) flashBackground()
})
} }
}).catch(error => { }).catch(error => {
scriptStatus.textContent = "Submission request error: " + (error && error.message || error) scriptStatus.textContent = "Submission request error: " + (error && error.message || error)
@ -83,7 +109,6 @@ if (q("#bibliogram-identifier-blocked")) {
scriptStatus.textContent = "Couldn't extract ID from page." scriptStatus.textContent = "Couldn't extract ID from page."
flashBackground(); flashBackground();
} }
})
} }
else if (res.status === 302) { else if (res.status === 302) {
@ -97,5 +122,5 @@ if (q("#bibliogram-identifier-blocked")) {
scriptStatus.textContent = "This profile doesn't exist." scriptStatus.textContent = "This profile doesn't exist."
flashBackground(); flashBackground();
} }
}) })
} }