Created
March 7, 2022 13:09
-
-
Save brahimmachkouri/53ec3dc4b262bbd93f1af969efec129c to your computer and use it in GitHub Desktop.
GM_xmlhttpRequest Promise
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
// GM_xmlhttpRequest GET | |
function getData(url, type = "document", usermethod = "GET") { | |
return new Promise((resolve, reject) => { | |
GM_xmlhttpRequest({ | |
method: usermethod, | |
url: url, | |
responseType: type, | |
onload: function (response) { | |
if (response.status == 200) { | |
resolve(response.response); | |
} else { | |
console.log("response:" + response.status); | |
reject(response.status); | |
} | |
}, | |
onerror: function (error) { | |
console.log("error"); | |
reject(error); | |
} | |
}); | |
}); | |
} | |
// GM_xmlhttpRequest POST | |
function postData(url, postData, type = "document", usermethod = "POST") { | |
return new Promise((resolve, reject) => { | |
GM_xmlhttpRequest({ | |
method: usermethod, | |
url: url, | |
headers: { | |
"Content-Type": "application/x-www-form-urlencoded" | |
}, | |
data: postData, | |
responseType: type, | |
onload: function (response) { | |
if (response.status == 200) { | |
resolve(response.response); | |
} else { | |
console.log("response:" + response.status); | |
reject(response.status); | |
} | |
}, | |
onerror: function (error) { | |
console.log("error"); | |
reject(error); | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment