简体   繁体   English

直接从浏览器执行JavaScript文件

[英]Executing a JavaScript file directly from the browser

This sounds like a trivia question but I really need to know. 这听起来像是一个琐事问题,但我真的需要知道。

If you put the URL of an HTML file in the Location bar of your browser, it will render that HTML. 如果您将HTML文件的URL放在浏览器的位置栏中,它将呈现该HTML。 That's the whole purpose of a browser. 这是浏览器的全部目的。

If you give it a JPG, or a SWF, or even PDF, it will do the right things for those datatypes. 如果你给它一个JPG,或一个SWF,甚至PDF,它将为这些数据类型做正确的事情。

But, if you give it the URL of a JavaScript file, it will display the text of that file. 但是,如果您给它一个JavaScript文件的URL,它将显示该文件的文本。 What I want is for that file to be executed directly. 我想要的是直接执行该文件。

Now, I know that if you use the javascript: protocol, it will execute the text of the URL, but that isn't what I need. 现在,我知道如果你使用javascript: protocol,它将执行URL的文本 ,但这不是我需要的。

I could have the URL point to an HTML file consisting of a single <script> tag that in turn points to the JavaScript file, but for occult reasons of my own, I cannot do that. 我可以将URL指向一个HTML文件,该文件由一个<script>标记组成,而该标记又指向JavaScript文件,但出于我自己的隐藏原因,我不能这样做。

If the file at http://example.com/file.js consists entirely of 如果http://example.com/file.js的文件完全由

 alert("it ran");

And I put that URL in the Location bar, I want "it ran" to pop up as an alert. 我把那个URL放在位置栏中,我希望“它运行”以弹出警报。

I'm skeptical that this is possible but I'm hoping-against-hope that there is a header or a MIME type or something like that that I can set and miraculously make this happen. 我怀疑这是可能的,但我希望 - 反对 - 希望有一个标题或MIME类型或类似的东西我可以设置并奇迹般地实现这一点。

This is not possible. 这是不可能的。 The browser has no idea what context the JavaScript should run in; 浏览器不知道JavaScript应该运行的上下文; for example, what are the properties of window ? 例如, window的属性是什么? If you assume it can come up with some random defaults, what about the behavior of document ? 如果你认为它可以提出一些随机的默认值,那么document的行为呢? If someone does document.body.innerHTML = "foo" what should happen? 如果有人做了document.body.innerHTML = "foo"会发生什么?

JavaScript, unlike images or HTML pages, is dependent on a context in which it runs. 与图像或HTML页面不同,JavaScript依赖于它运行的上下文。 That context could be a HTML page, or it could be a Node server environment, or it could even be Windows Scripting Host. 该上下文可以是HTML页面,也可以是Node服务器环境,也可以是Windows Scripting Host。 But if you just navigate to a URL, the browser has no idea what context it should run the script in. 但是如果你只是导航到一个URL,浏览器就不知道应该在哪个上下文中运行脚本。


As a workaround, perhaps use about:blank as a host page. 作为一种解决方法,也许使用about:blank作为主页。 Then you can insert the script into the document, giving it the appropriate execution context, by pasting the following in your URL bar: 然后,您可以通过在URL栏中粘贴以下内容将脚本插入到文档中,为其提供适当的执行上下文:

javascript:(function () { var el = document.createElement("script"); el.src = "PUT_URL_HERE"; document.body.appendChild(el); })();

Or you can use RunJS: https://github.com/Dharmoslap/RunJS 或者您可以使用RunJS: https//github.com/Dharmoslap/RunJS

Then you will be able to run .js files just with drag&drop. 然后你就可以通过拖放操作运行.js文件了。

Not directly, but you could make a simple server-side script, eg in PHP. 不是直接的,但你可以制作一个简单的服务器端脚本,例如在PHP中。 Instead of 代替

http://example.com/file.js

, navigate to: , 导航:

http://localhost/execute_script.php?url=http://example.com/file.js

Of course, you could smallen this by using RewriteRule in Apache, and/or adding another entry in your hosts file that redirects to 127.0.0.1 . 当然,您可以通过在Apache中使用RewriteRule和/或在主机文件中添加另一个重定向到127.0.0.1条目来解决这个问题。

Note that this is not great in terms of security, but if you use it yourself and know what you're downloading, you should be fine. 请注意,这在安全性方面并不是很好,但如果您自己使用它并知道您正在下载什么,那么您应该没问题。

<html>
 <head>

  <script>
   <? echo file_get_contents($_GET['url']); ?>
  </script>

 </head>

 <body>

 </body>
</html>

In the address bar, you simply write 在地址栏中,您只需编写

javascript:/ some javascript code here /;void(0); javascript:/ 这里有一些javascript代码 /; void(0);

http://www.javascriptkata.com/2007/05/01/execute-javascript-code-directly-in-your-browser/ http://www.javascriptkata.com/2007/05/01/execute-javascript-code-directly-in-your-browser/

Use Node.js. 使用Node.js. Download and install node.js and create a http/s server and write down what you want to display in browser. 下载并安装node.js并创建一个http / s服务器并记下要在浏览器中显示的内容。 use localhost::portNumber on server as url to run your file. 在服务器上使用localhost :: portNumber作为url来运行您的文件。 refer to node js doc - https://nodejs.org/dist/latest-v7.x/docs/api/http.html 请参阅节点js doc - https://nodejs.org/dist/latest-v7.x/docs/api/http.html

Run - http://localhost:3000 运行 - http:// localhost:3000

sample code below : 示例代码如下:

  var http = require("http");
  var server = http.createServer(function(req,res){
  res.writeHead(200,{'Content-Type':'text/html'});
       res.end("hello user");
  }); server.listen(3000);`

you can write your own browser using qt /webkit and do that. 您可以使用qt / webkit编写自己的浏览器并执行此操作。 when user enters a js file in url location you can read that file and execute the javascript . 当用户在url位置输入js文件时,您可以读取该文件并执行javascript。

http://code.google.com/apis/v8/get_started.html is another channel. http://code.google.com/apis/v8/get_started.html是另一个渠道。 not sure if it meets ur need. 不确定它是否符合你的需要。

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

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