Javascript 如何将jquery oembed返回的HTML全部作为字符串获取?

Javascript 如何将jquery oembed返回的HTML全部作为字符串获取?,javascript,jquery,oembed,Javascript,Jquery,Oembed,我正在测试,以解决我提出的问题。该脚本是加载在页面中的一个小javascript文件,并提供用于获取媒体嵌入代码的oembed函数 我可以很容易地从一个链接中获取oEmbed iframe,如下所示: <script> $(function(){ tag = $(document.createElement('div')).oembed('https://vimeo.com/40179668'); $('.header').append(tag); }); &

我正在测试,以解决我提出的问题。该脚本是加载在页面中的一个小javascript文件,并提供用于获取媒体嵌入代码的
oembed
函数

我可以很容易地从一个链接中获取oEmbed iframe,如下所示:

<script>
  $(function(){
    tag = $(document.createElement('div')).oembed('https://vimeo.com/40179668');
    $('.header').append(tag);
  });
</script>

$(函数(){
标记=$(document.createElement('div')).oembed('https://vimeo.com/40179668');
$('.header').append(标记);
});
这非常有效,并将oEmbed生成的iframe元素添加到页面上的
.header
。但是,我需要生成的HTML作为字符串。拨动
tag[0]。outerHTML
甚至
tag[0]。parent()[0]。outerHTML
刚刚显示
tag
是脚本中创建的原始空
div
,但它显然包含所有嵌入代码,因为嵌入的视频加载到页面上

如何将
oembed
函数返回的HTML作为文本字符串获取?我是否需要从创建的
div
标记遍历DOM才能找到它

编辑: 我添加了一些提醒,这都与时间有关。因为这是一个网络调用,所以在我查询
outerHTML
时,oEmbed函数还没有返回HTML块

编辑2:
看起来这可以通过JS回调来解决,因为oEmbed调用是异步的。当我有时间寻找解决方案时,我会把它作为答案贴回这里。

解决方案:

因为这是一个Rails应用程序,所以我决定在文本显示时而不是输入时使用该应用程序并解析视频链接。这对我来说是最好的解决方案,因为存储裸视频URL比存储oembed返回的完整嵌入iframe代码更能证明未来,并且希望它永远不会改变

这段代码很快就被破解了,需要很快进行重构,但它目前运行良好,失败对应用程序来说并不重要

在显示课程时,我运行
parse_media\u将
嵌入
课程。注意
查找所有链接,对于任何匹配的媒体站点,它将获取嵌入的代码(对于错误,使用
rescue
),并用iframe替换链接。完全令人讨厌,但它让我可以继续使用这个应用程序

我将在重构时发布新代码

查看:lesson.html.rb

...
<section class='lesson-notes'>
  <p><%= lesson.parse_media_embeds.html_safe %></p>
</section>
...
def parse_media_embeds
  # get an array of links in the notes
  links = URI.extract self.notes, /http(s)?/
  # due to wysihtml5 parsing each link is stored twice, once as url and once as text
  # like <a href='http://google.com'>http://google.com</a>
  # so remove the double references
  for pos in (0..links.count)
    link.delete_at(pos)
  end
  # now replace each link that matches the Regexp pattern with oembed version
  links.each do |link|
    # only replace links for YouTube, Vimeo and Soundcloud
    if link.match(/youtu\.be|youtube|vimeo|soundcloud/)
      # get the full link tag, which includes both instances of the link text
      string = /(<a\w*[^>]*>#{Regexp.escape(link)}[^<]*<\/a>)/
      embed = get_oembed(link)
      self.notes = self.notes.gsub(string, embed) if embed
    end
  end
  self.notes
end
def get_oembed(link)
  # each provider has a different set of params
  if link.match(/youtu\.be|youtube/)
    begin
      resource = OEmbed::Providers::Youtube.get(link, {maxwidth: 320})
    rescue
      return
    end
    resource.html
  elsif link.match(/vimeo/)
    begin
      resource = OEmbed::Providers::Vimeo.get(link, {maxwidth: 320})
    rescue
      return
    end
    resource.html
  elsif link.match(/soundcloud/)
    begin
      resource = OEmbed::Providers::SoundCloud.get(link, {color: '39d', show_comments: false})
    rescue
      return
    end
    resource.html
  else
    return
  end
end

你是不是碰巧找到了解决办法?@Natetronn我在下面添加了我的解决办法。它现在很粗糙,但它可以工作。我将很快重构此文件,并将发回。它还假设您正在Rails应用程序中执行此操作。