在不使用浏览器的情况下执行Javascript

在不使用浏览器的情况下执行Javascript

本文介绍了在不使用浏览器的情况下执行Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

在我正在开发的应用程序中,我从服务器下载我的网页,然后解析代码以获取信息。我使用 ASIHTTPRequest 从服务器获取代码。问题是,网页使用Javascript在页面上设置一些文本,并且 ASIHTTPRequest 从服务器拉取代码,所以Javascript从不被执行,因此网页是缺少信息。有没有一个简单的方法来执行Javascript没有浏览器,以便让它更新的代码?此外,目前我正在为OSX 10.9 NOT iOS开发应用程序。

In an application I'm working on, I am downloading a webpage of mine from the server and then parsing the code for information. I'm using an ASIHTTPRequest to get the code from the server. The problem is that the webpage uses Javascript to set some text on the page, and the ASIHTTPRequest pulls the code from the server, so the Javascript never gets executed, and therefore the webpage is missing information. Is there an easy way to execute the Javascript without a browser so as to let it update the code? Also, currently I'm developing the app for OSX 10.9 NOT iOS.

推荐答案

浏览器。浏览器正是呈现网页并让代码与其交互的软件。这不是 ASIHTTPRequest 能够做到的。

You cannot execute the javascript on a webpage without a browser. The browser is precisely the software that renders a webpage and lets code interact with it. This is not something ASIHTTPRequest will ever be able to do.

但是,您可以使用这基本上是一个无头和脚本的浏览器。

However, you can use PhantomJS which is basically a headless and scriptable browser. But packaging that within your app may be tricky.

或者,为了留在Cocoa领域,你可以加载一个WebView向上与你想要的页面,并永远不会显示它在屏幕上。然后,您可以在该WebView中执行javascript以返回数据。

Or possibly, to stay in Cocoa realm, you could load a WebView up with the page that you want, and never display it on screen. You can then execute javascript in that WebView to return data.

但是,这看起来不太好。下载一个页面,它的所有资产,解析和呈现它,然后执行javascript是一个缓慢和内存密集型操作。这将是如此清爽,如果你可以让你的服务器只是给你的数据,而不必运行任何复杂的客户端脚本。

However, this seems like a bad idea. Downloading a page, all it's assets, parsing and rendering it, then executing javascript is a slow and memory intensive operation. It will be so much cleaner if you can make your server just give you the data you want without having to run any complex client side scripts.

很难建议更多没有知道你正在尝试做什么,但是在这里为自己一个好处,并重新思考你的策略。

It's hard to advise more without knowing what you are trying to do, but do yourself a favor and rethink your strategy here.

这篇关于在不使用浏览器的情况下执行Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:44