简体   繁体   中英

Using addthis or external javascript to update div in polymer element shadow dom

I am trying to add some "addthis" buttons to an element in a polymer 2.0 app. I am able to import the "addthis" javascript but it seams the script itself needs to update a "div" that is part of a child element. The addthis script is looking for class="addthis_inline_share_toolbox" Is this possible? I think the problem is that the script cannot find the class in the shadow dom. How can I make that class available from the shadow dom so the script can find it? Is there a way to create this access through a polymer property?

<dom-module id="poem-card">
<template>
  <style>
    :host {
      display: block;
    }

    img {
      width: 100%
    }

    paper-card {
      --paper-card-header-text: {
        font-family: 'Fascinate Inline', cursive;
        color: yellow;
        font-size: xx-large;
      };
      --paper-card-header{
        position: 50%;
      };
    }
    .card-content *{
      margin: 8px 0;
      font-weight: bold;
      font-family: 'Alegreya Sans SC', sans-serif;

    }

  </style>
  <iron-ajax
    auto
    url="https://api.json"
    handle-as="json"
    last-response="{{response}}">
  </iron-ajax>
    <paper-card heading="{{response.items.0.title}}" image="https://placeimg.com/600/400/nature/sepia" alt="{{response.items.0.title}}">
      <div class="card-content">
        <p inner-h-t-m-l="{{response.items.0.content}}"></p>
      </div>
      <div class="card-actions">

        <div class="addthis_inline_share_toolbox"></div>
      </div>
    </paper-card>



</template>

<script>
  /**
   * `poem-card`
   * an individual poem in card form
   *
   * @customElement
   * @polymer
   * @demo demo/index.html
   */
  class PoemCard extends Polymer.Element {
    static get is() { return 'poem-card'; }
    static get properties() {
      return {
        prop1: {
          type: String,
          value: 'poem-card'
        }
      };
    }
  }

  window.customElements.define(PoemCard.is, PoemCard);
</script>

<script type="text/javascript" src="/addthis.js"></script>

code here

The idea of the shadow dom from Web Components is that no other script can change the Dom of a Web Compontnet. So to make this work, you need to give the external script an instance of the document of the Component like new addThis(Polymer.dom(this.root)) .
This is only half of the truth because this anwser is for polymer 2 if you use polymer 1 and tell it to use shady dom your script might just work fine because this restriction is only for shadow dom.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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