Created
February 11, 2017 23:53
-
-
Save stephentuso/aa71ad41ce62ef5b010805932d0a7a68 to your computer and use it in GitHub Desktop.
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
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