简体   繁体   English

如何获取脚本标记的innerHTML

[英]How to get a Script Tag's innerHTML

Alright... I've been searching for an hour now... How does one get the innerHTML of a script tag? 好吧......我一直在寻找一个小时......如何获得脚本标签的innerHTML? Here is what I've been working on... 这是我一直在努力的......

<script type="text/javascript" src="http://www.google.com" id="externalScript"></script>
<script type="text/javascript">
function getSource()
 {document.getElementById('externalScript').innerHTML;
  }
</script>

I've been trying to work on a way to call another domain's page source with the script tag. 我一直在尝试使用脚本标记调用另一个域的页面源。 I've seen a working example, but cannot find it for the life of me... 我见过一个有效的例子,但在我的生活中找不到它......

You can't do that. 你不能这样做。 There is no innerHTML....all you can do is pull down the file view XMLHttpRequest to get to its contents....but of course, that is limited by same-origin policy, but script tags are not. 没有innerHTML ....所有你能做的就是下拉文件视图XMLHttpRequest来获取它的内容....但是当然,这受到同源策略的限制,但是脚本标签不是。 Sorry. 抱歉。

I'm guessing you want one of two things: 我猜你想要两件事之一:

  1. To make a JavaScript file global (so that other pages can call it) 使JavaScript文件全局化(以便其他页面可以调用它)

  2. To get the script that is currently in the file 获取当前文件中的脚本

Both of those can be solved by moving your script to a .js file, and then using the tag 通过将脚本移动到.js文件,然后使用标记,可以解决这两个问题

<script src="[path-to-file]"></script>

actually, there is a way to get the content, but it depends on the remote server letting you get the file without valid headers and still fails a lot of the time just because of those settings. 实际上,有一种获取内容的方法,但它取决于远程服务器,让您获得没有有效标头的文件,并且由于这些设置仍然会失败很多时间。 using jQuery since it's the end of my day and I'm out the door.... 使用jQuery,因为这是我一天的结束,我出门了....

$.get($('#externalScript').attr('src'), function(data) {
    alert(data);
});

You can't do this. 你不能这样做。 It would be a massive security problem if you could. 如果可以的话,这将是一个巨大的安全问题。

Script content can include any number of things. 脚本内容可以包含任意数量的内容。 Consider this: a script loaded from a URL on your bank's website might contain all sorts of things, like your account number, your balance, and other personal information. 请考虑这一点:从银行网站上的URL加载的脚本可能包含各种内容,例如您的帐号,余额和其他个人信息。 That script would be loaded by your bank's normal pages to do what they want to do. 该脚本将由您银行的正常页面加载,以执行他们想要执行的操作。

Now, I'm an evil hacker, and I suspect you may be a customer of Biggo Bank. 现在,我是一个邪恶的黑客,我怀疑你可能是Biggo银行的客户。 So on one of my own pages, I include a <script> tag for that Biggo Bank script. 所以在我自己的一个页面上,我为Biggo Bank脚本添加了一个<script>标签。 The script may only load if there's a valid Biggo Bank session cookie, but what if there is? 只有在有效的Biggo银行会话cookie时才会加载脚本,但是如果有的话会怎样? What if you visit my hacker site while you're logged in to Biggo Bank in another browser tab? 如果您在另一个浏览器选项卡中登录Biggo Bank时访问我的黑客站点,该怎么办? Now my own JavaScript code can read the contents of that script, and your money is now mine :) 现在我自己的JavaScript代码可以读取该脚本的内容,你的钱现在是我的:)

You can Use Html Parsers: 您可以使用Html解析器:

jsoup » jsoup: Java HTML Parser jsoup: Java HTML Parser jsoup»jsoup:Java HTML Parser jsoup:Java HTML Parser

jsoup is a Java library for working with real-world HTML. jsoup是一个用于处理真实HTML的Java库。 It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods. 它提供了一个非常方便的API,用于提取和操作数据,使用最好的DOM,CSS和类似jquery的方法。

refer this: http://jsoup.org/ 请参考: http//jsoup.org/

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

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