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

39
LINES

< > BotCompany Repo | #1032645 // js_nodeScriptReplace (backup)

JavaX fragment (include)

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

Author comment

Began life as a copy of #1032354

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032645
Snippet name: js_nodeScriptReplace (backup)
Eternal ID of this version: #1032645/1
Text MD5: 1a321aaf92e9be7b29b1f67315c5d8d4
Author: stefan
Category: javax / javascript
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-30 02:45:12
Source code size: 1224 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 117 / 138
Referenced in: [show references]