Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

38
LINES

< > BotCompany Repo | #1032354 // js_nodeScriptReplace - use js_nodeScriptReplace2 instead

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (31L) is out of date.

// fixes <script> tags in inserted HTML (e.g. via innerHTML)
// TODO: Do we handle <script src=""> correctly?

sS js_nodeScriptReplace() {
  ret [[
    function nodeScriptReplace(node) {
      if (nodeScriptIs(node)) {
        node.parentNode.replaceChild(nodeScriptClone(node), node);
        console.log("nodeScriptReplace finished replacement");
      } else {
        var i = -1, children = node.childNodes;
        while (++i < children.length)
          nodeScriptReplace(children[i]);
      }
    
      return node;
    }
    
    function nodeScriptClone(node){
      var script  = document.createElement("script");
      if (node.src)
        console.log("nodeScriptReplace including " + node.src);
      else {
        console.log("nodeScriptReplace inline script");
        script.text = node.innerHTML;
      }

      var i = -1, attrs = node.attributes, attr;
      while ( ++i < attrs.length )
        script.setAttribute( (attr = attrs[i]).name, attr.value );
      return script;
    }
    
    function nodeScriptIs(node) {
      return node.tagName === 'SCRIPT';
    }
  ]];
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1032354
Snippet name: js_nodeScriptReplace - use js_nodeScriptReplace2 instead
Eternal ID of this version: #1032354/7
Text MD5: 4a17942db2ef1cf6d3d6d0d54c05ec46
Author: stefan
Category: javax / javascript
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-06 05:13:51
Source code size: 1132 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 157 / 239
Version history: 6 change(s)
Referenced in: [show references]