我想要追加下面从API调用中接收到的HTML标签。

huangapple go评论73阅读模式
英文:

I want to append below HTML tags which I recieved from an API call

问题

这是我从以下API调用中收到的内容:

<div id='rg_embed_link_437103' class='rg_embed_link' data-song-id='437103'>Read <a href='https://genius.com/Ed-sheeran-thinking-out-loud-lyrics'>“Thinking Out Loud” by Ed Sheeran</a> on Genius</div> <script crossorigin src='//genius.com/songs/437103/embed.js'></script>

lyricsContainer 在DOM中存在,但当我从API调用中附加歌词时,它只附加了"Read “Thinking Out Loud” by Ed Sheeran on Genius"部分。我想附加整个歌词部分。

async function fetchLyrics(songId) {
    let response = await fetch("https://api.genius.com/songs/" + songId + "?text_format=plain&access_token=xxx");
    let jsonData = response.json();
    let lyricData = await jsonData;
    console.log(lyricData.response.song.embed_content);
    $(".lyricsContainer").append(lyricData.response.song.embed_content);
}

我尝试在空白的HTML页面上附加了<div id='rg_embed_link_437103' class='rg_embed_link' data-song-id='437103'>Read <a href='https://genius.com/Ed-sheeran-thinking-out-loud-lyrics'>“Thinking Out Loud” by Ed Sheeran</a> on Genius</div> <script crossorigin src='//genius.com/songs/437103/embed.js'></script>,它显示了整个歌词部分。问题是当我尝试使用JavaScript附加相同的内容时。

英文:

This is what I recieved from the below API call..

<div id='rg_embed_link_437103' class='rg_embed_link' data-song-id='437103'>Read <a href='https://genius.com/Ed-sheeran-thinking-out-loud-lyrics'>“Thinking Out Loud” by Ed Sheeran</a> on Genius</div> <script crossorigin src='//genius.com/songs/437103/embed.js'></script>

The lyricsContainer does exists in the DOM , but when I append the lyrics from the API call it only appends the "Read “Thinking Out Loud” by Ed Sheeran on Genius" part.I want to append the whole lyrics part.

async function fetchLyrics(songId) {
    let response = await fetch("https://api.genius.com/songs/" + songId + "?text_format=plain&access_token=xxx")
    let jsonData = response.json();
    let lyricData = await jsonData;
    console.log(lyricData.response.song.embed_content);
    $(".lyricsContainer").append(lyricData.response.song.embed_content);


}

I tried appending the <div id='rg_embed_link_437103' class='rg_embed_link' data-song-id='437103'>Read <a href='https://genius.com/Ed-sheeran-thinking-out-loud-lyrics'>“Thinking Out Loud” by Ed Sheeran</a> on Genius</div> <script crossorigin src='//genius.com/songs/437103/embed.js'></script> on a blank HTML page and it did showed the entire lyrics section.The problem is when I tried to append the same using JavaScript.

答案1

得分: 0

以下是翻译好的部分:

Replace document.write

替换 document.write

请注意,这些脚本将允许 API 中的任何代码在您的站点范围内运行。

iframe document.write

请注意:第一个示例由于 iframe 限制而在 Stack Overflow 上无法工作,但在 jsfiddle 和您的站点上可以工作。

https://jsfiddle.net/mplungjan/unp2x8v0/

<!-- 开始代码片段:js 隐藏:false 控制台:true babel:false -->

<!-- 语言:lang-js -->
const result = `&lt;div id=&#39;rg_embed_link_437103&#39; class=&#39;rg_embed_link&#39; data-song-id=&#39;437103&#39;&gt;Read &lt;a href=&#39;https://genius.com/Ed-sheeran-thinking-out-loud-lyrics&#39;&gt;“Thinking Out Loud” by Ed Sheeran&lt;/a&gt; on Genius&lt;/div&gt; &lt;script crossorigin src=&#39;//genius.com/songs/437103/embed.js&#39;&gt;&lt;\/script&gt;`

let ifrm = document.getElementById(&#39;song&#39;);
try {
  let ifrWin = ifrm.contentWindow || ifrm.contentDocument.defaultView
  ifrWin.document.write(result);
  ifrWin.document.close();

} catch (e) {
  console.log(e.message)
}

/* 不工作
const [html,scriptStr] = result.split(&#39;&lt;script&#39;);
console.log(html)
const div = document.getElementById(&#39;mySong&#39;)
div.innerHTML = html;
const scriptSrc = scriptStr.match(/src=&#39;(.*?)&#39;/)[1]
console.log(scriptSrc);
const script = document.createElement(&#39;script&#39;);
script.setAttribute(&#39;crossorigin&#39;,true)
script.src=`https:${scriptSrc}`;
document.querySelector(&#39;head&#39;).appendChild(script);
// div.appendChild(script); // 也不工作
*/

<!-- 语言:lang-html -->
<iframe id=&quot;song&quot;>&lt;/iframe&gt;

<!-- 结束代码片段 -->

**替换 document.write**

```html
<!-- 开始代码片段:js 隐藏:false 控制台:true babel:false -->

<!-- 语言:lang-js -->
const result = `&lt;div id=&#39;rg_embed_link_437103&#39; class=&#39;rg_embed_link&#39; data-song-id=&#39;437103&#39;&gt;Read &lt;a href=&#39;https://genius.com/Ed-sheeran-thinking-out-loud-lyrics&#39;&gt;“Thinking Out Loud” by Ed Sheeran&lt;/a&gt; on Genius&lt;/div&gt; &lt;script crossorigin src=&#39;//genius.com/songs/437103/embed.js&#39;&gt;&lt;\/script&gt;`

const [html,scriptStr] = result.split(&#39;&lt;script&#39;);
console.log(html)
const div = document.getElementById(&#39;song&#39;)
div.innerHTML = html;
const scriptSrc = scriptStr.match(/src=&#39;(.*?)&#39;/)[1]
console.log(scriptSrc);
const script = document.createElement(&#39;script&#39;);
script.setAttribute(&#39;crossorigin&#39;,true)
script.src=`https:${scriptSrc}`;

const saveWrite = document.write;
document.write = str => div.innerHTML += str;

document.querySelector(&#39;head&#39;).appendChild(script);

<!-- 语言:lang-html -->
<div id=&quot;song&quot;>&lt;/div&gt;

<!-- 结束代码片段 -->
英文:

Genius uses document.write in their embed.js so we need to do the same or replace the write function

Do be aware that these scripts will allow any code from the API to run in the scope of your site.

iframe document.write

NOTE: The first example does not work at SO due to iframe restrictions but it works in a jsfiddle and on your site

https://jsfiddle.net/mplungjan/unp2x8v0/

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const result = `&lt;div id=&#39;rg_embed_link_437103&#39; class=&#39;rg_embed_link&#39; data-song-id=&#39;437103&#39;&gt;Read &lt;a href=&#39;https://genius.com/Ed-sheeran-thinking-out-loud-lyrics&#39;&gt;“Thinking Out Loud” by Ed Sheeran&lt;/a&gt; on Genius&lt;/div&gt; &lt;script crossorigin src=&#39;//genius.com/songs/437103/embed.js&#39;&gt;&lt;\/script&gt;`

let ifrm = document.getElementById(&#39;song&#39;);
try {
  let ifrWin = ifrm.contentWindow || ifrm.contentDocument.defaultView
  ifrWin.document.write(result);
  ifrWin.document.close();

} catch (e) {
  console.log(e.message)
}

/* does not work
const [html,scriptStr] = result.split(&#39;&lt;script&#39;);
console.log(html)
const div = document.getElementById(&#39;mySong&#39;)
div.innerHTML = html;
const scriptSrc = scriptStr.match(/src=&#39;(.*?)&#39;/)[1]
console.log(scriptSrc);
const script = document.createElement(&#39;script&#39;);
script.setAttribute(&#39;crossorigin&#39;,true)
script.src=`https:${scriptSrc}`;
document.querySelector(&#39;head&#39;).appendChild(script);
// div.appendChild(script); // does also not work
*/

<!-- language: lang-html -->

&lt;iframe id=&quot;song&quot;&gt;&lt;/iframe&gt;

<!-- end snippet -->

Replace document.write

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const result = `&lt;div id=&#39;rg_embed_link_437103&#39; class=&#39;rg_embed_link&#39; data-song-id=&#39;437103&#39;&gt;Read &lt;a href=&#39;https://genius.com/Ed-sheeran-thinking-out-loud-lyrics&#39;&gt;“Thinking Out Loud” by Ed Sheeran&lt;/a&gt; on Genius&lt;/div&gt; &lt;script crossorigin src=&#39;//genius.com/songs/437103/embed.js&#39;&gt;&lt;\/script&gt;`

   
const [html,scriptStr] = result.split(&#39;&lt;script&#39;);
console.log(html)
const div = document.getElementById(&#39;song&#39;)
div.innerHTML = html;
const scriptSrc = scriptStr.match(/src=&#39;(.*?)&#39;/)[1]
console.log(scriptSrc);
const script = document.createElement(&#39;script&#39;);
script.setAttribute(&#39;crossorigin&#39;,true)
script.src=`https:${scriptSrc}`;

const saveWrite = document.write;
document.write = str =&gt; div.innerHTML += str;

document.querySelector(&#39;head&#39;).appendChild(script);

<!-- language: lang-html -->

&lt;div id=&quot;song&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月25日 17:47:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76549784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定