Skip to content

Instantly share code, notes, and snippets.

@thdotnet
Created September 24, 2021 12:58
Show Gist options
  • Save thdotnet/4781ad32ab126da27af144ab6e90f928 to your computer and use it in GitHub Desktop.
Save thdotnet/4781ad32ab126da27af144ab6e90f928 to your computer and use it in GitHub Desktop.
load and interpret JS scripts
//sample.js
window.onload = function () {
var setInnerHTML = function (elm, html) {
elm.innerHTML = html;
Array.from(elm.querySelectorAll("script")).forEach(oldScript => {
const newScript = document.createElement("script");
Array.from(oldScript.attributes)
.forEach(attr => newScript.setAttribute(attr.name, attr.value));
newScript.appendChild(document.createTextNode(oldScript.innerHTML));
oldScript.parentNode.replaceChild(newScript, oldScript);
});
};
fetch(YOUR_URL_IN_HERE)
.then(function (response) {
return response.text();
})
.then(function (body) {
var dv = document.createElement('div');
setInnerHTML(dv, body);
//dv.innerHTML = body;
document.body.appendChild(dv);
setTimeout(function () {
eaddr.init();
}, 1000);
});
}();
//usage:
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>this is another web page</h1>
<script type='text/javascript' id='customScript' data-id='1234' src='sample.js'>
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment