Skip to content

Instantly share code, notes, and snippets.

@joelacummings
Last active July 5, 2019 17:24
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 joelacummings/294dfcdff998b7da94ecd41b641db7ad to your computer and use it in GitHub Desktop.
Save joelacummings/294dfcdff998b7da94ecd41b641db7ad to your computer and use it in GitHub Desktop.
(function(){
var base = "https://data.schemaapp.com/" + __schemaConfig.accountId + "/";
var url = __schemaConfig.strip_query ? btoa(unescape(window.location.href.replace(window.location.search, ""))).replace(/=/g, "") : btoa(unescape(window.location.href)).replace(/=/g, "");
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
if (xhr.responseText !== "") {
var ld = document.createElement('script');
ld.type = "application/ld+json";
ld.innerHTML = xhr.responseText;
document.getElementsByTagName('head')[0].appendChild(ld);
}
}
}
xhr.open("GET", base + url, true);
xhr.setRequestHeader("x-api-key", __schemaConfig.apiKey);
xhr.send();
})();
@joelacummings
Copy link
Author

<script>
    var __schemaConfig = __schemaConfig || {};
    __schemaConfig.accountId = "{{ $accountId }}";
    __schemaConfig.apiKey = "{{ $apiKey }}";
    __schemaConfig.strip_query = true; // only if strip


    (function() {
        var js = document.createElement('script');
        js.type = "text/javascript"; js.async = true;
        js.src="//cdn.schemaapp.com/javascript/cache.js";
        var f = document.getElementsByTagName("script")[0];
        f.parentNode.insertBefore(js, f);
    })();
</script>

@JohnM-729
Copy link

JohnM-729 commented Jul 5, 2019

I did something similar to this as a toy app last year. Sent you an invite as a collaborator if you'd like to inspect the code.
https://github.com/JohnM-729/foldout

It's a proof-of-concept that will remote load and invoke a vue app on a page when the user presses alt-shift-f.
Basically a similar approach to what you've got here, create a script node, set the source, and insert it, with the added frills of an event listener and interval to make sure that the vue library from cdn is loaded before invoking the app.

  • What you've got above look like it should work fine, however, I would replace the deprecated unescape function with decodeURI.

Also, don't forget that you can use eval() if you need to run a string as if it were JS code, if all else fails and you just want to run something and damn the torpedoes...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment