Created
June 9, 2021 08:55
-
-
Save Himura2la/40327c12e7a8b5bc093c0c5e238ee5fe to your computer and use it in GitHub Desktop.
Tampermonkey helpers
This file contains 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
// ==UserScript== | |
// ... | |
// @grant GM_xmlhttpRequest | |
// ==/UserScript== | |
(function () { | |
... | |
function httpRequestAsync(url, | |
handler, | |
data = null, | |
handlerExtraParam = null, | |
responseContentType = 'application/json') { | |
let request = new XMLHttpRequest() | |
request.onreadystatechange = e => { | |
if (e.target.readyState === 4 && Math.floor(e.target.status / 100) === 2) { | |
handler(e.target.responseText, handlerExtraParam) | |
} | |
} | |
request.open(data ? 'POST' : 'GET', url, true) | |
request.setRequestHeader('Content-Type', responseContentType); | |
request.send(data) | |
} | |
function objectToQueryString(obj) { | |
return Object.keys(obj) | |
.map(key => `${key}=${encodeURIComponent(obj[key])}`) | |
.join('&') | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment