Skip to content

Instantly share code, notes, and snippets.

@stephentuso
Created February 11, 2017 23:53
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 stephentuso/aa71ad41ce62ef5b010805932d0a7a68 to your computer and use it in GitHub Desktop.
Save stephentuso/aa71ad41ce62ef5b010805932d0a7a68 to your computer and use it in GitHub Desktop.
function injectPage(url) {
var CONTAINER_SELECTOR = '.content';
//Show loading indicator
document.querySelector(CONTAINER_SELECTOR).innerHTML = '<center>Loading...</center>';
url = decodeURI(url);
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'document';
xhr.overrideMimeType("text/html; charset=utf-8");
xhr.onloadend = function(e) {
if (e.target.status != 200) {
// error handling
return;
}
var injectDoc = e.target.response;
var meta = injectDoc.head.querySelector('meta[itemprop="name"]');
if (meta) {
var metaContentName = injectDoc.head.querySelector('meta[itemprop="name"]').content;
document.head.querySelector('meta[itemprop="name"]').content = metaContentName;
}
// Inject article body.
var container = document.querySelector(CONTAINER_SELECTOR);
var newDocContainer = injectDoc.querySelector(CONTAINER_SELECTOR);
$(container).html(newDocContainer.innerHTML);
// Scroll to top of page
window.scrollTo(0, 0);
};
xhr.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment