- Are these things legal?
- If they are, how so?
TL;DR: They aren't illegal, which is not the same thing.
All around the world, there's laws against unauthorized use of a computer, but all of them imply that you "enter" inside the computer by bypassing something and that you install something on it ; so, that you "pirate it" and put a malware. But here there's no piracy, the only thing bypassed is the laws themselves.
Obviously there's something put on your computer, but technically speaking it's not installed since it stay on the RAM (the web cache is a help, not a necessity). Same, technically it don't goes against your consent since all the web browser let you deactivate the use of JavaScript ; globally or for some specific sites.
So, like I said it's not illegal... But this just because there is no law authorizing, or forbidding, to temporary hijack few resources of a stranger computer without bypassing the security and without putting something inside the computer.
It's the equivalent of a real life, "can I borrow your phone for few seconds", without asking first. Take the phone in your target pocket, make your call and put it back in the pocket. There's no laws forbidding you to temporary use the phone of someone, but it you get caught doing it, you'll be facing the justice for robbery.
You'll defend yourself by a, "I thought he was okay with that", and depending of the situation you'll win or not. If it's someone really near to you, like you son/father by example, you can probably win. Not because it's a near family member (it's still robbery even in this case), but because the relation imply some implicit consent for temporary use of things.
The exact same thing apply here : I thought he was okay with me mining some bitcoins since he let me run some script. You know, he use my site, let me send him ads and have a "me want money" button. He must know that I need money for this service and like there's no harms by mining, I sincerely thought he was okay with this.
Here's the "implicit consent". You know offering a service have a cost. You also know that you use it for free and still (more or less) contribute financially speaking with the help of the ads. In the same time, more and more ads are flash animations or videos ; they temporary use the resources for your computer. So, you should have nothing against a more harmless and no visually invading way to financially help.
This said, with the help of a good lawyer and a good expert in computer, you perhaps can win if you decide to sue a site using some mining feature against your will. But who will do this ? If it were a real big site with millions of people using it each days for long times (facebook, twitter, amazon, etc.) you probably can find someone willing to take his chance, but here...
"Hey, you know this site, for 30 seconds it was mining bitcoins using the resources of my computer. I'll definitively sue them for this ! It will take years and I'll have to sell my wife and children, but I don't care, I'll do it ! And I don't care that I was using this site for some illegal activity, and so that I can backfire at anytime."
Now, you can do otherwise. Just add a, "oh, by the way I'll use your computer to mines some bitcoins while you're here. If you disagree, just click here and I'll stop", and suddenly it's completely legal. You'll probably be seen as an asshole, you'll probably loose tons of users, but it will be legal.