Issue #1148πŸ’¬ AnsweredOpened May 31, 2018by dwengticorp0 reactions

Script in component get messed up, any idea?

Quick answerby artf

If you use any kind of minifier, the script is affected too. You can set the script as a string if you need to show the complete code to the user

Read full answer below ↓

Question

Code SnippetTEXT
I created a component like 

  const TWITTER_TYPE = 'TWITTER';
  domc.addType(TWITTER_TYPE, {
    model: defaultModel.extend({
      defaults: {
        ...defaultModel.prototype.defaults,
        droppable: false,
        account: 'OntarioAlliance',
        widget_width: 425,
        widget_height: 525,
        traits: [
          { label: 'Account', name: 'account', changeProp: 1, },
          { label: 'Width', name: 'widget_width', type: 'number', changeProp: 1, },
          { label: 'Height', name: 'widget_height', type: 'number', changeProp: 1, }
        ],
        script: function() {
          if(this.innerHTML.length == 0){
            this.innerHTML=`<a class="twitter-timeline" data-width="426" data-height="525" data-dnt="true" href="https://twitter.com/{[ account ]}?ref_src=twsrc%5Etfw">Tweets by {[ account ]}</a>`;

            if(window.twttr && window.twttr.widgets)
              twttr.widgets.load();
            else
            (function(d, s, id) {       
              var js, fjs = d.getElementsByTagName(s)[0]; 
              if (d.getElementById(id)){
                twttr.widgets.load();
                return;
              }
              fjs = d.getElementsByTagName(s)[0];
              js = d.createElement(s); js.id = id; 
              js.src = "https://platform.twitter.com/widgets.js"; 
              fjs.parentNode.insertBefore(js, fjs);
              }(document, "script", "twitter-widgets"));  
          }
        }
      },
    }, {
      isComponent(el) {
        if(el.getAttribute &&
          el.getAttribute('data-gjs-type') == TWITTER_TYPE) {
          return {
            type: TWITTER_TYPE
          };
        }


but after I put it in page and check the export html, I see code like below


<div id="idtj">
</div>
<script>var items = document.querySelectorAll('#idtj');
  for (var i = 0, len = items.length; i < len; i++) {
    (function(){
      0==this.innerHTML.length&&(this.innerHTML='<a class="twitter-timeline" data-width="426" data-height="525" data-dnt="true" href="https://twitter.com/OntarioAlliance?ref_src=twsrc%5Etfw">Tweets by OntarioAlliance</a>',window.twttr&&window.twttr.widgets?twttr.widgets.load():function(e,t,n){
        var a,i=e.getElementsByTagName(t)[0];
        if(e.getElementById(n))return void twttr.widgets.load();
        i=e.getElementsByTagName(t)[0],a=e.createElement(t),a.id=n,a.src="https://platform.twitter.com/widgets.js",i.parentNode.insertBefore(a,i)}
                                 (document,"script","twitter-widgets"))}
     .bind(items[i]))();
  }
</script>

See the code after the function(), it messes the page up.

Any idea?

Answers (2)

artfβ€’ May 31, 2018

If you use any kind of minifier, the script is affected too. You can set the script as a string if you need to show the complete code to the user

lock[bot]β€’ September 17, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins β†’
Premium option

Premium plugins ship with support, regular updates, and production-ready features β€” save days of integration work.

Browse premium plugins β†’

Related tutorials

In-depth guides on the same topic.

All tutorials β†’

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.