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
<!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. | |
* |
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
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) |