I already found a similar post here:
register-javascript-for-document-library
But instead of adding a content/script editor to every document library, as suggested, I'd prefer to add my code to the system master page and have it triggered only when a document library gets opened. Does any one have a solution for this? The code I developed is working great when I put it in a script editor, now how can I make it run from the master page only on document libraries?