You can only do this if you:
-
Control the websites in question (in which case, just serve the code on those sites), or
-
Control/extend the browser being used. In this case, if you write a browser addon/extension, you can configure it to run when the browser goes to specific websites.
What you can’t do is have website A (which you control) install JavaScript code that will run when a user (using a browser that doesn’t have your addon/extension) goes to website B (which you don’t) — for obvious security reasons. (Or rather, you can’t do it without the user expressly chosing to by installing your addon/extension.)
3
solved Restrict some JS to work on specific websites [closed]