简体   繁体   English

在第三方域上调用JavaScript

[英]Invoke JavaScript on 3rd party domain

I want to write some javascript and have it call into the DOM for a page I am loading from a 3rd party domain. 我想写一些javascript并让它调用DOM从我从第三方域加载的页面。 Can this be done? 可以这样做吗? This looks like what I've already tried using IFRAME but it would seem that doesn't work. 看起来像我已经尝试使用IFRAME但它似乎不起作用。 Is these some other way like having FF run some javascript directly rather than as part of a page? 这些是其他方式,如FF直接运行一些JavaScript而不是作为页面的一部分?

I know this has all kinds of security problems but I'm the guy writing the code and the only guy who will run it. 我知道这有各种各样的安全问题,但我是编写代码的人,也是唯一能够运行代码的人。


The backstory: I'm trying to automate some web site iterations. 背景故事:我正在尝试自动化一些网站迭代。

My fist IFRAME pass didn't work because a web page from file:////.... is not in the same domain as a page in http://whatever.com . 我的拳头IFRAME传递不起作用,因为来自file:////....的网页与http://whatever.com的页面不在同一个域中。 Surprise, surprise. 惊喜,惊喜。

If I understand the question correctly, you probably won't be able to do it using Javascript alone, because of the domain restriction that you experienced. 如果我正确理解了这个问题,您可能无法单独使用Javascript,因为您遇到了域限制。 However, if you have some knowlege on using shell scripts, or any scripting language, it should be no problem, all you need to do is invoke the good old curl. 但是,如果您对使用shell脚本或任何脚本语言有一些了解,那么它应该没问题,您需要做的就是调用好的旧卷曲。

Example in PHP: PHP中的示例:

<?php
$url = "http://www.example.com/index.html";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
$fp = curl_exec($ch);
curl_close($ch);
?>

And that's pretty much it. 这就是它。 You have the actual HTML code in the $fp variable. 您在$ fp变量中有实际的HTML代码。 So, all in all, what I would do is write a little Javascript Ajax function to PHP which does the curl and then returns the $fp variable via echo to the Javascript callback, and then maybe insert it on the document (using innerHTML or the DOM), and bam, you have access to all the stuff. 总而言之,我要做的是给PHP编写一个小的Javascript Ajax函数,它执行curl然后通过echo将$ fp变量返回给Javascript回调,然后将其插入到文档中(使用innerHTML或者DOM)和bam,你可以访问所有的东西。 Or you could just parse it in PHP. 或者你可以用PHP解析它。 Either way, should work fine if you do it through curl. 无论哪种方式,如果你通过卷曲做它应该工作正常。 Hope that helps. 希望有所帮助。

Edit : After some thought I seem to remember that Safari removes the cross domain restriction for localhost. 编辑 :经过一番思考我似乎记得Safari删除了localhost的跨域限制。 After researching some more, I'm unable to find any documentation that supports this theory of mine, so I dug a little deeper and found a better (although hackier) way to accomplish this whole mess via Apache if you're using it (which you probably are). 在研究了一些之后,我找不到任何支持我这个理论的文档,所以我挖得更深一点,找到了一个更好的(虽然更黑客)的方式来通过Apache完成这个混乱,如果你正在使用它(你可能是)。

Apache's mod_proxy will take a request for something like “/foo” and actually tunnel the request to some remote destination like “ http://dev.domain.com/bar ”. Apache的mod_proxy将接受类似“/ foo”的请求,并实际将请求隧道传送到某个远程目标,如“ http://dev.domain.com/bar ”。 The end result is that your web browser thinks you've made a call to http://localhost/foo but in reality you're sending and retrieving data from a remote server. 最终结果是您的Web浏览器认为您已经调用了http:// localhost / foo,但实际上您正在从远程服务器发送和检索数据。 Security implications solved! 安全问题解决了!

Example: 例:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so

Let's assume that I want to access a file at http://dev.domain.com/remote/api.php . 我们假设我想访问http://dev.domain.com/remote/api.php上的文件。 You would put all of the following into a : 您可以将以下所有内容放入:

# start mod_rewrite
RewriteEngine On
ProxyRequests Off
<Proxy>
   Order deny,allow
   Allow from all
</Proxy>

ProxyPass /apitest/ http://dev.domain.com/remote/api/
ProxyPassReverse /apitest/ http://dev.domain.com/remote/api/
RewriteRule ^/apitest/(.*)$ /remote/api/$1 [R]

Source 资源

More edit: 更多编辑:

Seeing as how you want to avoid the whole server setup thing, I gave it a shot using an IFRAME on Safari (Mac), and it worked, at least for the domains I tried: 看到你想要避免整个服务器设置的事情,我在Safari(Mac)上使用IFRAME进行了一次拍摄,并且它起作用了,至少对于我尝试过的域:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html>
<head>
</head>
    <body>
        <iframe src="http://www.stackoverflow.com/"></iframe>
    </body>
</html>

Read up on bookmarklets . 阅读bookmarklets The basic idea is you create a bookmark that executes some Javascript code that dynamically injects Javascript into the page currently loaded in your browser. 基本思想是创建一个书签,执行一些Javascript代码,动态地将Javascript注入到浏览器中当前加载的页面中。 Most of the web page clipping applications do this. 大多数网页剪辑应用程序都这样做。

JavaScript has a same domain policy. JavaScript具有相同的域策略。 You are not going to be able to access the other domain. 您将无法访问其他域。 It is to protect you have hackers/bad people. 这是为了保护你有黑客/坏人。

Take a look at Selenium Remote-Control . 看看Selenium Remote-Control The server acts as a proxy for your browser to bypass the same-domain policy: 服务器充当您的浏览器的代理,以绕过同一域策略:

Finally, the Selenium Server acts as a client-configured HTTP proxy, to stand in between the browser and your website. 最后,Selenium Server充当客户端配置的HTTP代理,位于浏览器和您的网站之间。 This allows a Selenium-enabled browser to run JavaScript on arbitrary websites. 这允许支持Selenium的浏览器在任意网站上运行JavaScript。

You might consider applying the same approach and writing your own proxy or even a simple web app that echoes pages from other domains (see Dave's answer ). 您可以考虑应用相同的方法并编写自己的代理,甚至是一个简单的Web应用程序,它可以回应其他域中的页面(请参阅Dave的回答 )。

Or, simply use Selenium for your automation. 或者,只需使用Selenium进行自动化。

There is a way to relax Firefox's domain security. 有一种方法可以放松Firefox的域安全性。

1 Add this line to Firefox's user.js. 1将此行添加到Firefox的user.js.

user_pref("signed.applets.codebase_principal_support", true); user_pref(“signed.applets.codebase_principal_support”,true);

2 Add this line to every javascript function that needs to cross a domain. 2将此行添加到需要跨域的每个javascript函数中。

netscape.security.PrivilegeManager.enablePrivilege( "UniversalBrowserRead UniversalBrowserWrite" ); netscape.security.PrivilegeManager.enablePrivilege(“UniversalBrowserRead UniversalBrowserWrite”);

3 The first time Firefox attempts to cross the domain, it will warn you of the attempt and prompt for your permission. 3 Firefox第一次尝试越过域名时,它会警告您尝试并提示您获得许可。

Good news, the bug that prevented this from working with Firefox 3 appears to be fixed. 好消息是,阻止它使用Firefox 3的错误似乎已得到修复。

I'm not sure I fully understand the issue, maybe you could describe the situation more ....but I'm guessing you're running into cross-site-scripting security problems if you are accessing across domains. 我不确定我是否完全理解这个问题,也许你可以更多地描述这种情况......但是如果你跨域访问,我猜你正在遇到跨站点脚本的安全问题。

So.. 所以..

maybe checkout the document.domain property which can enable script access across window objects in most browsers. 也许签出document.domain属性,它可以在大多数浏览器中启用跨窗口对象的脚本访问。

Both sites must be accessed via the same main domain, but can have different sub-domains so long as document.domain is set to the "main" part of the domain on both sites. 两个站点必须通过相同的主域访问,但只要document.domain设置为两个站点上域的“主”部分,就可以拥有不同的子域。

Not what I was thinking of but: iMacros might do some of what I want. 不是我想到的,但是: iMacros可能会做我想要的一些事情。

After looking it seems a bit limited and the docs are a bit to much bling and not enough meat. 看之后它看起来有点受限制,而且文档有点多,但没有足够的肉。

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

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