luciopaiva / witchcraft

Inject Javascript and CSS right from your file system. Think GreaseMonkey for more advanced users.
https://luciopaiva.com/witchcraft
MIT License
254 stars 18 forks source link

How can I inject JS if my server is something like `localhost:8888/`? #18

Closed alessandro308 closed 4 years ago

alessandro308 commented 4 years ago

I often develop using IntelliJ web server that creates a local webserver and permits to open a page using `http://localhost:63342/? In that case, I have to create a file that match that url but a file with a colon in the name results as invalid file name. How can I manage that issue?

luciopaiva commented 4 years ago

That's an excellent question, @alessandro308. I will add it to the FAQ section.

Witchcraft matches the hostname, not the host. As the MDN docs explain, hostname doesn't take into account the port part.

So, in your case, you can simply create localhost.js and it will work fine.

If you must match the port (you could be running another web server locally and you don't want to run the script there), you can check the port before running your actual script. Checking the port can be done by reading from location.port. So you could just:

if (location.port === 63342) {
    // run my script
}

You could even go one step further and create a separate file for that specific server and include it from localhost.js, just like this:

if (location.port === 63342) {
    // @include "my-intellij-web-server-script.js"
}

So that's it. I hope that solves your problem. Let me know if you need more help. For now, I'm closing this issue.

luciopaiva commented 4 years ago

Question added to the FAQ.