Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brahimmachkouri/53ec3dc4b262bbd93f1af969efec129c to your computer and use it in GitHub Desktop.
Save brahimmachkouri/53ec3dc4b262bbd93f1af969efec129c to your computer and use it in GitHub Desktop.
GM_xmlhttpRequest Promise
// 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