Sure thing. None of the following code is tested, but it should help.
1. Add an "upload a script" to include on the page function
First you need to modify your page class. You could either create a new page type ("PageWithJavaScript" maybe) or add this to your normal page class if you want it available for all of them.
First, modify your page model so it has:
static $has_one = array(
'JavaScriptFile' => 'File'
);
then add an upload field to the getCMSfields:
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main.Content', new FileIFrameField('JavaScriptFile', 'JavaScript Behaviours'));
return $fields;
}
Finally, in your page controller init function:
function init() {
parent::init();
if($this->JavaScriptFile()->exists())
Requirements::javascript($this->JavaScriptFile()->getRelativePath());
}
2. Add a plain text field where optional JS can be entered
Just add a text column to your database and a text area field in the CMS and use Requirement::customScript instead, in the init function.
3. Create a set of 'safe' scripts as separate dataobjects...
Requires a bit more work, but you should be able to do this if you've done the tutorials and you understand what is happening above.
You'll need to understand how to attach events to objects without using inline tags. For example, instead of:
<a id='someLink' onclick='return someComplexFunction()' href='/'>Link</a>
you should be doing stuff like:
<a id='someLink' href='/'>Link</a>
...
<script type='text/javascript'>
document.getElementById('someLink').onclick = function() {
// etc
}
</script>
Hope this helps.