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.

1  
// fixes <script> tags in inserted HTML (e.g. via innerHTML)
2  
// TODO: Do we handle <script src=""> correctly?
3  
4  
sS js_nodeScriptReplace() {
5  
  ret [[
6  
    function nodeScriptReplace(node) {
7  
      if (nodeScriptIs(node)) {
8  
        node.parentNode.replaceChild(nodeScriptClone(node), node);
9  
        console.log("nodeScriptReplace finished replacement");
10  
      } else {
11  
        var i = -1, children = node.childNodes;
12  
        while (++i < children.length)
13  
          nodeScriptReplace(children[i]);
14  
      }
15  
    
16  
      return node;
17  
    }
18  
    
19  
    function nodeScriptClone(node){
20  
      var script  = document.createElement("script");
21  
      if (node.src)
22  
        console.log("nodeScriptReplace including " + node.src);
23  
      else {
24  
        console.log("nodeScriptReplace inline script");
25  
        script.text = node.innerHTML;
26  
      }
27  
28  
      var i = -1, attrs = node.attributes, attr;
29  
      while ( ++i < attrs.length )
30  
        script.setAttribute( (attr = attrs[i]).name, attr.value );
31  
      return script;
32  
    }
33  
    
34  
    function nodeScriptIs(node) {
35  
      return node.tagName === 'SCRIPT';
36  
    }
37  
  ]];
38  
}

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: 160 / 243
Version history: 6 change(s)
Referenced in: [show references]