Could you insert invisible characters into the HTML code so if one copies it, it would be a pain to remove all those characters that would cause syntax errors but you wouldn't ever notice it unless you copied into a text editor. They would be randomized characters between every actual code character.
Visible code:
function foo()
Actual HTML:
f!u#n%c^t&i*o(n) (f@o$o%(^)&
Where each of the symbols are invisible unicode characters. I don't know how many of those there are but I tried the ZERO WIDTH NO-BREAK SPACE and it seemed to work. It would be an inconvenience. That's the best we can do, inconvenience, there is no foolproof way to stop somebody from copying the code.
To clarify, to anyone reading the page, they see the first line. To somebody who copied the text into a text editor, they'll either see squares or nothing but the code should not compile.