Move most JavaScript code into separate library

Adjusted some scopes too, and some lint updates.