简体   繁体   English

跨域Javascript书签

[英]Cross Domain Javascript Bookmarklet

I've been at this for several days and searches including here haven't give me any solutions yet. 我已经待了几天了,搜索(包括此处)还没有给我任何解决方案。

I am creating a Bookmarklet which is to interact with a POST API. 我正在创建一个与POST API交互的Bookmarklet。 I've gotten most of it working except the most important part; 除了最重要的部分,我已经完成了大部分工作; the sending of data from the iframe (I know horrible! If anyone knows a better solution please let me know) to the javascript on my domain (same domain as API so the communication with the API is no problem). 从iframe(我知道这太可怕了!如果有人知道更好的解决方案,请让我知道)将数据发送到我域(与API相同的域,因此与API的通信没有问题)上的javascript。

From the page the user clicks on the bookmarklet I need to get the following data to the javascript that is included in the iFrame. 用户从页面上单击小书签,我需要将以下数据获取到iFrame中包含的javascript。

    var title = pageData[0].title;
    var address = pageData[0].address;
    var lastmodified = pageData[0].lastmodified;
    var referralurl = pageData[0].referralurl;

I first fixed it with parsing this data as JSON and sending it through the name="" attribute of the iFrame but realized on about 20% of webpages this breaks. 我首先通过将数据解析为JSON并将其通过iFrame的name =“”属性发送的方式进行了修复,但在大约20%的此中断的网页上都意识到了这一点。 I get an access denied; 我的访问被拒绝; also it's not a very pretty method. 这也不是一个非常漂亮的方法。

Does anyone have anyidea on how I can solve this. 有谁对我该如何解决有任何想法。 I am not looking to use POSTS that redirect I want it all to be AJAX and as unobtrusive as possible. 我不想使用重定向的POSTS,我希望所有这些都是AJAX并尽可能不引人注目。 It's also worth noting I use the jQuery library. 还值得注意的是,我使用的是jQuery库。

Thank you very much, 非常感谢你,

Ice

You should look into easyXDM , it's very easy to use. 您应该研究easyXDM ,它非常易于使用。 Check out one of the examples on http://consumer.easyxdm.net/current/example/methods.html http://consumer.easyxdm.net/current/example/methods.html上查看示例之一

After a lot of work I was able to find a solution using JSONP which is enables Cross Domain Javascript. 经过大量工作,我能够找到使用JSONP的解决方案,该解决方案启用了跨域Javascript。 It's very tricky with the Codeigniter Framework because passing data allong the URLs requires a lot of encoding and making sure you dont have illegal characters. 使用Codeigniter框架非常棘手,因为沿URL传递数据需要大量编码,并确保您没有非法字符。 Also I'm still looking to see how secure it really is. 另外,我仍在寻找它的真正安全性。

If I understand your question correctly, you might have some success by looking into using a Script Tag proxy. 如果我正确理解了您的问题,那么通过使用脚本标签代理可能会取得一些成功。 This is the standard way to do cross domain AJAX in javascript frameworks like jquery and extjs. 这是在JavaScript框架(如jquery和extjs)中进行跨域AJAX的标准方法。

See Jquery AJAX Documentation 请参阅Jquery AJAX文档

If you need to pass data to the iframe, and the iframe is actually including another page, but that other page is on the same domain (a lot of assumptions, I know). 如果您需要将数据传递到iframe,并且iframe实际上包含另一个页面,但是该另一个页面位于同一域中(我知道很多假设)。

Then the man page code can do this: 然后,手册页代码可以做到这一点:

DATA_FOR_IFRAME = ({'whatever': 'stuff'});

Then the code on the page included by the iframe can do this: 然后,iframe包含的页面上的代码可以执行以下操作:

window.parent.DATA_FOR_IFRAME;

to get at the data :) 获取数据:)

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

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