简体   繁体   English

在Android 4.4 Webview中使用Shadow-dom(聚合物)

[英]Using shadow-dom (Polymer) in Android 4.4 Webview

I'm actually building a web app using custom Polymer elements (which is based on web-components and shadow-dom) but I encountered a serious problem. 我实际上是在使用自定义Polymer元素(基于Web组件和shadow-dom)构建Web应用程序,但是遇到了一个严重的问题。

I want this app to run on an Android 4.4 WebView, but I experienced some issues with the shadow-dom when testing the app on the WebView, although it works perfectly fine on Chrome (Android and Desktop). 我希望此应用程序可以在Android 4.4 WebView上运行,但是在WebView上测试该应用程序时,虽然我在Shadow-dom上遇到了一些问题,尽管它在Chrome(Android和台式机)上运行良好。 This problem occurs when I try to access a shadowRoot element from CSS (with ::shadow or /deep/) or from pure js. 当我尝试从CSS(使用:: shadow或/ deep /)或从纯js访问shadowRoot元素时,会发生此问题。

Here is a simple illustration of my problem : 这是我的问题的简单说明:


<!-- custom-elt.html -->
<link rel="import" href="../bower_components/polymer/polymer.html">

<polymer-element name="custom-elt">

    <template>
        <div id="elt">some content</div>
    </template>

    <script>
        Polymer('custom-elt',{
            getElt: function(){
                return this.shadowRoot.getElementById('elt');
            }
        });
    </script>

 </polymer-element>

// script.js
window.addEventListener('polymer-ready',function(){
    var e = document.getElementsByTagName('custom-elt')[0];
    alert(e.getElt().innerHTML);
});

Works perfectly on Chrome, but not in the WebView. 可以在Chrome上完美运行,但不能在WebView中运行。 According to caniuse.com, shadow-dom is supported by Android 4.4 browser (which is used by the WebView right ?) with the 'webkit' prefix ( http://caniuse.com/#feat=shadowdom ), but I didn't find a way to use it with the prefix. 根据caniuse.com的说法,具有“ webkit”前缀( http://caniuse.com/#feat=shadowdom )的Android 4.4浏览器(由WebView使用吗?)支持shadow-dom,但我没有这样做。找不到使用前缀的方法。

Any solution ? 有什么办法吗?

Thanks 谢谢

Baptiste 巴蒂斯特

This was on the polymer site: 这是在聚合物站点上:

On browsers without native shadow DOM, use the wrap function to access methods and properties on your element: 在没有本地影子DOM的浏览器上,使用wrap函数访问元素上的方法和属性:

wrap($0).fire('my-event'); wrap($0).myproperty

Here is more documentation on the shadowDOM and polyfill: https://www.polymer-project.org/docs/polymer/debugging.html#shadowdom 这是有关shadowDOM和polyfill的更多文档: https ://www.polymer-project.org/docs/polymer/debugging.html#shadowdom

That leads me to ask, are you including the current polyfill? 这使我问,您是否包括当前的polyfill? https://github.com/furier/WebComponents https://github.com/furier/WebComponents

It just changed recently with the 0.5 build and is a breaking change. 它最近在0.5版本中进行了更改,这是一个重大更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM