简体   繁体   English

通过index.html头中的API调用设置脚本标签

[英]Set script tag from API call in header of index.html

I'm trying to implement dynatrace in my react app, and this works fine if I just add the required script tag manually in my index.html header. 我正在尝试在我的react应用程序中实现dynatrace,并且如果我只是在index.html标头中手动添加所需的脚本标记,则此方法会很好地工作。

However I want to make use of dynatraces api which returns the whole script tag element (so I can use for different environments). 但是我想使用dynatraces api,它返回整个脚本标签元素(因此我可以用于不同的环境)。

How can I add the script tag to my index.html after calling the api? 调用api后如何将脚本标签添加到index.html? Creating a script element from code won't work because the response of the api call is a script tag itself (which is returned as string). 从代码创建脚本元素将不起作用,因为api调用的响应本身就是脚本标记(作为字符串返回)。

I tried creating a div element and adding the script as innerHtml, then append it to the document. 我尝试创建div元素并将脚本添加为innerHtml,然后将其附加到文档中。 But scripts don't get executed in innerHtml text. 但是脚本不会在innerHtml文本中执行。

const wrapperDiv = document.createElement("div");
wrapperDiv.innerHTML = "<script>alert('simple test')</script>";
document.head.appendChild(wrapperDiv.firstElementChild);

Can this be done? 能做到吗?

I found a roundabout way of doing this: 我发现了一种回旋方式:

const wrapperDiv = document.createElement("div");
const scriptElement = document.createElement("script");
wrapperDiv.innerHTML = "<script src=... type=...></script>";
for(let i = 0; i < wrapperDiv.firstElementChild.attributes.length; i++){
    const attr = wrapperDiv.firstElementChild.attributes[i];
    scriptElement.setAttribute(attr.name, attr.value);
}
document.head.appendChild(scriptElement);

in this example of the script i'm only using a src but this can be done with the value as well. 在此脚本示例中,我仅使用src,但这也可以使用该值来完成。 If there is any better way for doing this pls let me know 如果有更好的方法可以做到这一点,请告诉我

This can be achieved without use of eval() : 这可以在不使用eval()情况下实现:

const source = "alert('simple test')";
const wrapperScript = document.createElement("script");
wrapperScript.src = URL.createObjectURL(new Blob([source], { type: 'text/javascript' }));
document.head.appendChild(wrapperScript);

In the code above you basically create Blob, containing your script, in order to create Object URL (representation of File or Blob object in browser memory). 在上面的代码中,您基本上创建了包含脚本的Blob,以便创建对象URL(浏览器内存中File或Blob对象的表示形式)。 This solution is based on idea that dynamically added <script> is evaluated by browser in case it has src property. 此解决方案基于以下想法:如果动态添加的<script>具有src属性,则浏览器会对其进行评估。

Update: 更新:

Since endpoint returns you <script> tag with some useful attributes, the best solution would be to clone attributes (including src ) - your current approach is pretty good. 由于端点返回具有一些有用属性的<script>标记,因此最好的解决方案是克隆属性(包括src )-您当前的方法非常好。

I found a roundabout way of doing this: 我发现了一种回旋方式:

const wrapperDiv = document.createElement("div"); const wrapperDiv = document.createElement(“ div”);

const scriptElement = document.createElement("script");
wrapperDiv.innerHTML = "<script src=... type=...></script>";
for(let i = 0; i < wrapperDiv.firstElementChild.attributes.length; i++){
    const attr = wrapperDiv.firstElementChild.attributes[i];
    scriptElement.setAttribute(attr.name, attr.value);
}
document.head.appendChild(scriptElement);

in this example of the script i'm only using a src but this can be done with the value as well 在此脚本示例中,我仅使用src,但是也可以使用该值来完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM