You're right - escaping is very dependent on context, and sometimes you need to escape data multiple times in nested contexts.
In inline script you need to get JS string escaping right and avoid </ sequence that ends HTML CDATA.
In inline script you need to get JS string escaping right and avoid </ sequence that ends HTML CDATA.
FAIL :-P
AFAIK You need to JS escape anything that goes into a JS context too.