【问题标题】:Losing JavaScriptCore bindings on page reload在页面重新加载时丢失 JavaScriptCore 绑定
【发布时间】:2014-05-12 12:02:29
【问题描述】:

我在 webview 中使用 Javascriptcore 创建了 c++ 绑定,因此我的 c++ fns.并且可以从 html 访问对象。 我已按照本教程创建这些绑定。 http://parmanoir.com/Taming_JavascriptCore_within_and_without_WebView

在重新加载页面之前,我在创建和使用绑定方面没有问题。 一旦我在我的 html 页面中调用“location.reload()”,我就会丢失所有的绑定。

示例 html 代码为:

<html>

<head>
    <style type="text/css">
    body {
        background-color: transparent;
    }
    p {
        color: black;
        background-color: rgb(255, 0, 255);
        position: absolute;
        top: 0;
        left: 5;
        margin: 0;
        padding: 0;
    }
    </style>

    <script type="text/javascript" language="JavaScript1.4"><!--

    function testDummyModule()
    {
        if ( window.Dummy ) 
        {   
            console.log("*** Dummy ID: " + window.Dummy.id);

        }
        else 
        {
            console.log("[!] Dummy not supported.");

        }

    }

    function init()
    {
        console.log("Testing Dummy");
        testDummyModule();
        console.log("Finished testing Dummy");
        location.reload();

    }

    </script>
</head>
    <body onload="init();">
        <!--<p> This Page is loading without any problems</p>
        <br>-->
        <div id="test"></div>
        <div id="text">Test Page</div>
    </body>
</html>

在调用 location.reload 后重新加载页面时,我收到错误消息:“Dummy,不支持。”

我尝试在收到 setUrl 调用后立即重新创建绑定(通过删除正在创建绑定并再次创建它们的 c++ 类),但仍然出现相同的错误。

任何线索,我怎样才能使绑定再次可用

【问题讨论】:

    标签: webkit javascriptcore


    【解决方案1】:

    通过重新加载页面,webkit 会为该新页面接收一个新的“全局”上下文,并且您的绑定必须添加到这个新上下文中。应该不需要删除绑定并重新创建它们,只需将它们添加到这个新上下文中即可。

    我不确定您的 C++ 代码是什么样的,但例如,我刚刚向我的 Javascript 绑定类添加了一个重新绑定函数,该函数从 webkit 接收新上下文并重新绑定“虚拟”对象。

    void JsDummy::rebind(JSGlobalContextRef ctx)
    {
        // m_jsDummyClassRef is a JSClassRef created and stored in the JsDummy class
        JSObjectRef JsDummyObjectRef = JSObjectMake(ctx, m_jsDummyClassRef, this);
    
        JSObjectRef globalObjectRef = JSContextGetGlobalObject(ctx);
        JSStringRef DummyName = JSStringCreateWithUTF8CString("Dummy");
    
        // Attach the JavaSript object "Dummy" to the new global context
        JSObjectSetProperty(ctx, globalObjectRef, DummyName, JsDummyObjectRef, kJSPropertyAttributeDontDelete, 0);
        JSStringRelease(DummyName);
    }
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-11-09
      • 2014-01-27
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多