Skip to content

Instantly share code, notes, and snippets.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML Importer Demo</title>
<script>
/**
* Synchronously fetches the given URL and writes its contents
* into the document at this script’s position.
*
function fetchXHR (url, httpMethod, body) { // httpMethod is optional
var xhr = new XMLHttpRequest();
var promise = new Promise(function (resolve, reject) {
xhr.onreadystatechange = function () {
if (xhr.readyState !== 4) return
if (xhr.status >= 200 && xhr.status < 300) resolve(xhr.response)
else reject({status: xhr.status, statusText: xhr.statusText, xhr:xhr})
}
xhr.open(httpMethod || 'GET', url, true)
xhr.send(body)