georgemandis / konami-js

Adding the Konami Code easter egg to your projects since 2009! Compatible with gestures on smartphones and tablets as well. Compatible with all front-end frameworks and vanilla JavaScript
http://konamijs.mand.is/
MIT License
959 stars 122 forks source link

Calling another Javascript file from a URL? #59

Closed virtadpt closed 3 years ago

virtadpt commented 3 years ago

Hi. Would it be possible to use konami-js to pull in and run another Javascript file sourced from a URL? Here is what I tried, but all it does is display the Javascript code in the browser tab when I run it:

<script src="{{ SITEURL }}/theme/assets/js/konami.js"></script>
<script type="text/javascript">
    var kitteh = new Konami("https://webneko.net/n20171213.js");
</script>

Note: I can redirect to a different URL entirely without trouble.

I don't want to pull down a copy of the file and self-host it because the author of Webneko asks folks to not do that, and I want to respect his wishes.

georgemandis commented 3 years ago

Hi @virtadpt! The default behavior for konami-js, when you pass a URL in as a parameter, is to redirect the user there. If you'd like to have it do something else you can pass a callback function instead. Something like this:

const kitteh = new Konami(() => { const script = document.createElement('script'); script.type = 'text/javascript'; script.src = '"https://webneko.net/n20171213.js'; document.getElementsByTagName('head')[0].appendChild(script); });

I'm not quite sure how the Webneko script works, so you might need to take additional steps after the file loads.

Let me know if that helps?

virtadpt commented 3 years ago

I just gave it a try on my dev server and it didn't seem to work. Nothing in my dev server output (which displays all requests made for debugging purposes. However in the Firefox Javascript console I get the following messages:

A call to document.write() from an asynchronously-loaded external script was ignored.
Use of captureEvents() is deprecated. To upgrade your code, use the DOM 2 addEventListener()
    method. For more help http://developer.mozilla.org/en/docs/DOM:element.addEventListener

And no appearance of webneko, unfortunately.

georgemandis commented 3 years ago

Ah, it looks like Webneko uses document.write() to insert something into the page. That means you're probably not going to be able to dynamically load that script after the page is already loaded, at least not without copying and rewriting it a bit, against the will of the author.

A possibly less satisfying, hacky way to do it though might be to:

I looked at the demo site and I think this should work:

// hide Neko right after you load it
document.querySelector('#layerNeko0').style.display = "none"

// Inside the Konami-JS callback, unhide it:
const kitteh = new Konami(() => {  
document.querySelector('#layerNeko0').style.display = "block";
});

Does that make sense?

virtadpt commented 3 years ago

It does make sense, and I thank you. I was working with the author of Webneko and he came up with a simpler method: https://github.com/virtadpt/pelican-html5up-striped/blob/master/templates/index.html#L46

georgemandis commented 3 years ago

Makes sense! Thanks for using this silly project :)