Skip to content

Instantly share code, notes, and snippets.

@Himura2la
Created June 9, 2021 08:55
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 Himura2la/40327c12e7a8b5bc093c0c5e238ee5fe to your computer and use it in GitHub Desktop.
Save Himura2la/40327c12e7a8b5bc093c0c5e238ee5fe to your computer and use it in GitHub Desktop.
Tampermonkey helpers
// ==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