javascript - 解析 jsbin 输出 html 文档以获取 'javascript' 字段内容的方法

标签 javascript html parsing dom

我这里有一个jsbin https://jsbin.com/wanegavisa/edit?html,js,console我正在尝试使用nodejs下载输出html https://output.jsbin.com/wanegavisa并解析出 javascript 中的内容jsbin 编辑器中的选项卡。我打算使用像 Cheerio 这样的 dom 解析器并获得第一个 <script> body 内的标签。我刚刚看到可以将脚本标签直接添加到正文中。

如何解析这样的文档才能得到包含

的字符串
console.log('hi')

我可以从 src 等于此 url https://static.jsbin.com/js/render/edit.js 的脚本中选择同级脚本。但如果该网址发生变化,那么我就不走运了。

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
  <meta charset="utf-8">
<!--
Created using JS Bin
http://jsbin.com

Copyright (c) 2016 by anonymous (http://jsbin.com/wanegavisa/2/edit)

Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>

  <script>console.log('meow')</script>

<script>
console.log('hi')
</script>
<script src="https://static.jsbin.com/js/render/edit.js?3.35.12"></script>
<script>jsbinShowEdit && jsbinShowEdit({"static":"https://static.jsbin.com","root":"https://jsbin.com"});</script>
<script src="https://static.jsbin.com/js/vendor/eventsource.js?3.35.12"></script>
<script src="https://static.jsbin.com/js/spike.js?3.35.12"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-1656750-34', 'jsbin.com');
ga('require', 'linkid', 'linkid.js');
ga('require', 'displayfeatures');
ga('send', 'pageview');

</script>

</body>
</html>

最佳答案

jsbin 有一个 api! :)

https://jsbin.com/api/wanegavisa

{
  "javascript": "console.log('hi')",
  "html": "<!DOCTYPE html>\n<html>\n<head>\n<script src=\"https://code.jquery.com/jquery-1.9.1.js\"></script>\n  <meta charset=\"utf-8\">\n  <meta name=\"viewport\" content=\"width=device-width\">\n  <title>JS Bin</title>\n</head>\n<body>\n\n  <script>console.log('meow')</script>\n  \n</body>\n</html>",
  "css": "",
  "settings": {
    "processors": {
      "html": "html",
      "css": "css",
      "javascript": "javascript"
    },
    "title": "JS Bin"
  },
  "last_updated": "2012-07-23T00:00:00.000Z",
  "url": "wanegavisa",
  "snapshot": 2
}

关于javascript - 解析 jsbin 输出 html 文档以获取 'javascript' 字段内容的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36826873/

相关文章:

Javascript 和 Ruby on Rails

javascript - 如何使用代码在asp web表单中最后聚焦textarea

javascript - 在组件中处理通过 react-router 传递的参数

javascript - HTML 5 延迟属性

parsing - 快速解析允许转义字符的字符串?

python - 有没有办法检查字符串是否包含未知格式的日期?

javascript - 嵌套 jQuery UI 可排序

javascript - 带有图像的 html 字符串未加载

javascript - 使用 JQuery 追加和幻灯片

c++ - 使用 istream 迭代器时解析失败