document.location.href //returns the URL in the location field of browser (string)
With that in mind, all we need is a piece of code that compares this URL with a list of permitted ones, and if non of the later matches the former, we know the library is being accessed from an outside source (and take appropriate action). Suffice it to say such a code will need to be added directly inside the library in question in order to take effect.
Another solution for this- if you can handle it- is via server side scripting, specifically, .htaccess. Let's look at that now.