英文:
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 = `<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>`
let ifrm = document.getElementById('song');
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('<script');
console.log(html)
const div = document.getElementById('mySong')
div.innerHTML = html;
const scriptSrc = scriptStr.match(/src='(.*?)'/)[1]
console.log(scriptSrc);
const script = document.createElement('script');
script.setAttribute('crossorigin',true)
script.src=`https:${scriptSrc}`;
document.querySelector('head').appendChild(script);
// div.appendChild(script); // 也不工作
*/
<!-- 语言:lang-html -->
<iframe id="song"></iframe>
<!-- 结束代码片段 -->
**替换 document.write**
```html
<!-- 开始代码片段:js 隐藏:false 控制台:true babel:false -->
<!-- 语言:lang-js -->
const result = `<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>`
const [html,scriptStr] = result.split('<script');
console.log(html)
const div = document.getElementById('song')
div.innerHTML = html;
const scriptSrc = scriptStr.match(/src='(.*?)'/)[1]
console.log(scriptSrc);
const script = document.createElement('script');
script.setAttribute('crossorigin',true)
script.src=`https:${scriptSrc}`;
const saveWrite = document.write;
document.write = str => div.innerHTML += str;
document.querySelector('head').appendChild(script);
<!-- 语言:lang-html -->
<div id="song"></div>
<!-- 结束代码片段 -->
英文:
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 = `<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>`
let ifrm = document.getElementById('song');
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('<script');
console.log(html)
const div = document.getElementById('mySong')
div.innerHTML = html;
const scriptSrc = scriptStr.match(/src='(.*?)'/)[1]
console.log(scriptSrc);
const script = document.createElement('script');
script.setAttribute('crossorigin',true)
script.src=`https:${scriptSrc}`;
document.querySelector('head').appendChild(script);
// div.appendChild(script); // does also not work
*/
<!-- language: lang-html -->
<iframe id="song"></iframe>
<!-- end snippet -->
Replace document.write
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const result = `<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>`
const [html,scriptStr] = result.split('<script');
console.log(html)
const div = document.getElementById('song')
div.innerHTML = html;
const scriptSrc = scriptStr.match(/src='(.*?)'/)[1]
console.log(scriptSrc);
const script = document.createElement('script');
script.setAttribute('crossorigin',true)
script.src=`https:${scriptSrc}`;
const saveWrite = document.write;
document.write = str => div.innerHTML += str;
document.querySelector('head').appendChild(script);
<!-- language: lang-html -->
<div id="song"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论