代码之家  ›  专栏  ›  技术社区  ›  Un1

如何使disqus注释javascript代码在聚合物自定义元素中工作

  •  1
  • Un1  · 技术社区  · 9 年前

    我正在使用聚合物初学者工具包2.0

    嵌套自定义元素的结构:

    (意思是 <my-app></my-app> 在索引内。html等):

    | index.html
    --------\ my-app.html (自定义元素)
    ----------------\ my-testView1.html(我的测试视图1.html) (自定义元素)
    ----------------\ my-testView2.html(我的测试视图2.html) (自定义元素)

    我需要在my-testView1.html和my-testView 2.html页面上添加disqus注释(每个页面都有单独的线程)

    我试图让它工作,chrome的控制台消息告诉我必须使用ajax方法(因为my-testView1.html和my-testView 2.html都在里面 <iron-pages> 我想这就是为什么会有路由选择),它也给了我这个链接 https://help.disqus.com/customer/portal/articles/472107-using-disqus-on-ajax-sites

    结构 my-testView1.html :

    (我将example.com和myChannelID改为实名)

           ...
             <content select=".disqus1"></content>
          </template>
    
          <script>
            Polymer({
              is: 'my-testView1',
    
          )};
        </script>
    
        <script>
          var disqus_config = function () {
            this.page.url = 'https://www.example.com/testView1'; 
            this.page.identifier = '/testView1'; 
            this.page.title = 'Test View1';
            };
    
    
         (function() { 
            var d = document, s = d.createElement('script');
            s.src = 'https://MyChannelId.disqus.com/embed.js';
            s.setAttribute('data-timestamp', +new Date());
            (d.head || d.body).appendChild(s);
            })();
    
          DISQUS.reset({
              reload: true,
              config: function () {
                this.page.identifier = "/testView1";
                this.page.url = "https://www.example.com/#!testView1";
            }
            });
        </script>
    </dom-module>
    

    结构 my-testView2.html (与第一个相同):

       ...
          <content select=".disqus2"></content>
      </template>
    
      <script>
        Polymer({
          is: 'my-testView2',
    
        });
      </script>
      <script>
         var disqus_config = function () {
            this.page.url = 'https://www.example.com/testView2'; 
            this.page.identifier = '/testView2'; 
            this.page.title = 'Test View2';
            };
    
    
         (function() { 
            var d = document, s = d.createElement('script');
            s.src = 'https://MyChannelId.disqus.com/embed.js';
            s.setAttribute('data-timestamp', +new Date());
            (d.head || d.body).appendChild(s);
            })();
    
          DISQUS.reset({
              reload: true,
              config: function () {
                this.page.identifier = "/testView2";
                this.page.url = "https://www.example.com/#!testView2";
            }
            });
        </script>
    

    Disqus comments don't work in a polymer custom element
    在那里,我得到了一些帮助和指示,终于做到了这一点。但我仍然无法使它与ajax一起工作,这样它就可以将单独的disqus线程加载到每个页面。

    #! 或者我必须把它插进去 ready: function() {} 但如果我这样做,它只会破坏布局,所以我把它放在一个单独的标签中,现在它会显示 DISQUS is not defined

    我只有50英镑的荣誉奖金可以分享,但如果你知道如何让它发挥作用,请你解释一下如何修复它。

    2 回复  |  直到 9 年前
        1
  •  2
  •   Supersharp    9 年前

    这个 <div id="disqus_thread"> 元素在页面中必须是唯一的,因为disqus库将在该元素中安装下载的线程。所以您应该在 <iron-pages> 标签

    <div>
      <iron-pages>
        <view-1></view-1>
        <view-2></view-2>
      </iron-page‌​s>
    </div>
    <div id="disqus_thread"></div>
    

    然后你必须打电话 DISQUS.reset() 每次显示子页面时。你可以知道,因为 iron-selected 添加到选定元素中。所以你可以听听 attributeChange() 回调Polymer元素,并检查它是否包含 选定的铁 班如果是真的,你可以打电话 DISQUS.reset()

    Polymer( {
        is: 'view-1',
        attributeChanged: function ( name, old, value )
        {
            if ( name == 'class' && this.classList.contains( 'iron-selected' ) )
            {   
                //call DISQUS.reset() here
                DISQUS.reset( {
                    reload: true,
                    config: function () 
                    {
                        this.page.identifier = "/testView1"
                        this.page.url = "https://www.example.com/testView1"
                    }
                } )
            }
        }
    } ) 
    

    DISQUS.reset() ,您可以删除 var disqus_config = ...


    为了解决 <div id=disqus_thread> 要素:

    如果要将其插入右侧页面内,可以使用 appendChild() 在呼叫之前移动它 DISQUS.reset() 例如,把它放在 <div id='container'> 要素

    在…内 属性更改() 方法:

    this.$.container.appendChild( document.getElementById( 'disqus_thread' ) )
    

    view-1 <template> :

    <template>
      //Page content
      <div id=container></div>
      //Page footer
    </template>
    
        2
  •  1
  •   Community Mohan Dere    6 年前

    另一种可能的解决方案是:

    创建一个html文件并将disqus代码放入其中:

    <div id="disqus_thread"></div>
    <script>
        var disqus_shortname = 'YourDisqusId';
        var disqus_identifier = '/view1';
        var disqus_url = 'https://example.com/view1/';
        var disqus_config = function () {
    
        };
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = 'https://' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
         })();
    
    </script>
    

    然后将iframe添加到链接该代码的自定义元素页面

     <iframe src="../src/page1_disqus.html" style="width:100%; min-height:400px; border:0 none;"></iframe>
    

    如果它只是在这个iframe中显示您的应用程序,那么最好不要将这个html文件放在src目录中,而是放在单独的服务器上,使用共享链接而不是本地存储 ../src/

    如果需要在多个页面上放置disqus,只需创建另一个html文件,然后将其链接到需要使用iframe的页面

    推荐文章