Changed BMJSBridge to remember the current context, and compare it and check if it's changed.
There are cases that even after the context has been changed - window object has been cleared, [_object JSObject] does not become NULL, possibly because BMJSBridge retains _object.
This happens 'sometimes,' but definitely when [_webFrame globalContext] or [_webFrame windowObject] is called before the window object is actually ready.
As in WebKit/webkit/Source/WebCore/bindings/js/ScriptControllerMac.mm:98, the function generates a root object on demand. I suspect there's something different than usual going on.
Changed BMJSBridge callMethod:withArguments: to throw an exception when _script returns non-object.
Changed BM.js to try-catch window.localStorage.
When the page is file:// and trying to access localStorage, SecurityError gets thrown.
This happens if a user presses any media key before the page loads - when it's still about:blank.
Changed ajax~settings to BM~ajax, _isListeningToRefresh to _isListeneningForTokens for clarity.