How to dynamically include javascript into chalk pages

Our Custom HTML scripts total 25 scripts with 4600 lines of code now so I would like to create a plugin for the admin context like the Custom HTML form that keeps scripts separated. But, how do I take the javascript entered on the form and get it included as a script resource for set of Confluence pages? This plugin needs to be able to include any arbitrary script on demand.

Any help or suggestions would be appreciated.