我的应用程序使用假网址只是为了我们理解www.myapp.com需要在假网址www.myredirect.com处生成一个弹出窗口。
一旦到达www.myredirect.com ,页面就会自动重定向到另一个 url,我们将其称为www.myfinalurl.com。Www.myredirect.com 服务确保将页面重定向到www.myfinalurl.com,并添加一些查询字符串,例如www.myfinalurl.com?myparam=thisvalue。
我是www.myfinalurl.com和www.myapp.com的所有者,因此我可以在两者中编写 javascript 代码。
我需要从www.myapp.com捕获URL 中包含的www.myfinalurl.com的查询字符串,但是由于跨源策略,我无法直接执行此操作,因此我必须使用 postmessages 来执行此操作。
然而,一旦打开www.myredirect.com的窗口,可能会要求用户登录,这个过程需要不固定的秒数,所以我必须等待重定向发生才能继续。此外,www.myapp.com是在会话(www.myapp.com/s/mysession123)中执行的,这意味着它的 url 不是固定的。
该过程通常是:
然而,这里的问题是,虽然流程通常可以正常工作,但在我的情况下,步骤 1 必须在正确的时间发送,但是它会丢失,因为当重定向仍然存在时,消息将被发送到不存在的 Web 服务即将发生。
我在这里询问是否有可能的解决方法,以及您认为最好的解决方法是什么。
我想也许我可以每隔 x 秒将初始的 postmessage 从 myapp.com 发送到 myfinalurl.com ,直到我收到回复。这被认为是一种不错的做法吗?
所有可能的内置侦听器似乎都被跨源策略阻止。
当然,我尝试询问不同的人工智能,但还没有找到答案。
这是我迄今为止最好的尝试,在发送消息之前等待 …
我使用几行javascript来创建一个iframe元素,然后我想给它发一条消息,如下所示:
function loadiframe (callback) {
var body = document.getElementsByTagName('body')[0];
var iframe = document.createElement('iframe');
iframe.id = 'iframe';
iframe.seamless = 'seamless';
iframe.src = 'http://localhost:3000/iframe.html';
body.appendChild(iframe);
callback();
}
loadiframe(function() {
cpframe = document.getElementById('iframe').contentWindow;
cpframe.postMessage('please get this message','http://localhost:3000');
console.log('posted');
})
Run Code Online (Sandbox Code Playgroud)
然后,在http:// localhost:3000/iframe.html(iframe的源代码)里面是这样的:
<!DOCTYPE html>
<html>
<head>
<title>does not work</title>
<script>
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
if (event.origin == "http://localhost:3000") {
document.write(JSON.stringify(event));
document.write(typeof event);
}
}
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但没有任何反应......我甚至试图不使用原产地的安全检查,但即使没有发生任何事情......就像从来没有得到消息......
我在某种异常问题吗?我试图确保在postMessage消失之前加载了iframe ...
EDIT1:此外,控制台上没有显示错误......
EDIT2:我在Google Chrome 11和Firefox 4中尝试过它
先感谢您.
我正在寻找一种方法将鼠标点击发送到Windows上的后台应用程序(即通过句柄),我用来确认我的代码工作的测试窗口接受并处理点击,但我的目标应用程序没有(尽管间谍++)显示消息).
可能是什么导致了这个?是否有解决方法?
这是我正在使用的C#代码.
public enum WMessages : int
{
WM_LBUTTONDOWN = 0x201,
WM_LBUTTONUP = 0x202,
WM_KEYDOWN = 0x100,
WM_KEYUP = 0x101,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14,
}
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern int PostMessage(HandleRef hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
public void SendClick(WMessages type, Point pos)
{
switch(type)
{
case WMessages.WM_LBUTTONDOWN:
PostMessage(new HandleRef(null, this.process.MainWindowHandle),
(UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
(IntPtr)((pos.Y << 16) | (pos.X & 0xFFFF)));
return;
case WMessages.WM_LBUTTONUP:
PostMessage(new HandleRef(null, this.process.MainWindowHandle),
(UInt32)WMessages.WM_LBUTTONDOWN, (IntPtr)0x1,
(IntPtr)((pos.Y << 16) …Run Code Online (Sandbox Code Playgroud) 我看了几种方法,我不确定哪种方法最好.
我有2个域 - 完全相同,除了不同的协议.
父页面已打开http,子iframe已启用https.
我需要从子节点向父节点发送3个变量.
是什么以最简单的方式实现了这一目标?postMessage与IE7后备?例如:http://benalman.com/projects/jquery-postmessage-plugin/
还是jsonp?
通过context-menu模块,我检测用户点击了哪个DOM元素我的自定义上下文菜单项"Mark":
var menu = require("context-menu").Item({
label: "Mark",
contentScriptFile: data.url("context.js"),
onMessage: function (node) {
//Send the node to page-mod
}
});
Run Code Online (Sandbox Code Playgroud)
context.js:
self.on("click", function (node, data) {
self.postMessage(node);
});
Run Code Online (Sandbox Code Playgroud)
现在我想将此节点引用发送到一个page-mod模块,其中注入了pagemod的contentScript的每个页面都知道我点击的节点(并在每个选项卡中标记带有红色边框的HTML元素).
我知道postMessage()无法将消息发送到pagemod ,所以如何让这些模块进行通信?有优雅的worker解决方案吗?
我在页面上有2个iframe,我正在尝试使用postMessage将DOM元素从一个iframe传递到另一个iframe.但铬一直给我错误:
"DataCloneError:无法克隆对象."
有没有解决这个问题的方法?
我正在寻找一种方法来获取iframe contentWindow对象,并在用户的某些操作后向其发布消息.我目前的解决方案根本没有角度感觉(特别是从控制器访问DOM).
我创建了一个问题来证明这个问题:http://plnkr.co/edit/aXh4jydWGWfK3QQD4edd
执行postMessage是一种更有棱角的方式吗?
控制器:
app.controller('Main', function($scope) {
$scope.click = function() {
var iframe = document.getElementById("inner").contentWindow;
iframe.postMessage("Hello iframe", '*');
}
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<body ng-controller="Main">
<button ng-click="click()">send message</button>
<iframe id="inner" src="inner.html"/>
</body>
Run Code Online (Sandbox Code Playgroud) 我试图通过OAuth2在弹出窗口中使用Constant Contact进行身份验证.我$.postMessage用来在窗口之间发送数据,并且在大多数情况下,它工作得很漂亮.
我的问题是Safari.普通请求的URL如下所示:
https://example.com/oauth-v2/#access_token=xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx&token_type=Bearer&expires_in=xxxxxxxxx
但是,当使用Safari发出请求,整个哈希被切断的URL和location.hash,window.location.hash,window.parent.location.hash都是空的.
身份验证流程相当标准:
这是我们用来获取URL哈希信息的javascript
jQuery(document).ready(function ($) {
$.extend({
getQueryParameters: function (str) {
return (str || document.location.search || document.location.hash)
.replace(/(^\?)|(^\#)/, '')
.split("&")
.map(function (n) { return n = n.split("="), this[n[0]] = n[1], this }.bind({}))[0];
}
});
$.receiveMessage(function (event) {
$.postMessage($.getQueryParameters(), event.origin, event.source);
setTimeout(function () {
window.close()
}, 5000);
});
});
Run Code Online (Sandbox Code Playgroud)
丢失的哈希值是Safari中的已知错误吗?我应该做些什么来从Constant Contact获取信息吗?它适用于所有其他浏览器,所以我不想重写这部分应用程序.
我正努力postMessage()在iframe和我的主网站之间进行沟通.但是,使用MDN上的示例代码中给出的确切语法,我遇到了一个很好的错误.我尝试了几件事,比如在Javascript中初始化iframe并将其附加到我的页面,但这给我留下了同样的错误.同样有单独的选择器来选择我的iframe.Undefined is not a function
我有以下Javascript代码:
<script type="text/javascript">
$(document).ready(function() {
$('.editor').postMessage("A", "domain here");
});
function receiveMessage(event)
{
if (event.origin !== "domain here")
return;
// Do something
}
window.addEventListener("message", receiveMessage, false);
</script>
Run Code Online (Sandbox Code Playgroud)
上面的脚本尝试向页面上的iframe发送消息,如下所示:
<iframe src="domain here/frameListener.html" class="editor"></iframe>
Run Code Online (Sandbox Code Playgroud)
然后它具有receiveMessage捕获作为对主网页的响应而发送的任何消息的功能.最后但并非最不重要的是,我已经尝试了这个问题中给出的答案:但这并没有解决我的问题.因此,它不是重复的.
如何摆脱此错误消息?
我有一个iframe,它正在与其父窗口进行通信以通过postMessage 方法设置和获取一些必要的cookie 。
首先,iframe应用程序中的一个函数从父窗口请求CookieA。
function requestCommunication(topic, customerId) {
function cookieAvailable() {
return new Promise((resolve) => resolve(getCookieData('cookieName'));
});
}
console.log(cookieAvailable());
if(!!cookieAvailable()) {
//doStuff
}
}
Run Code Online (Sandbox Code Playgroud)
cookieAvailable()触发从iframe发送到parent.window的消息。依次,窗口返回cookie及其数据作为字符串。通过使用以下操作完成此操作:
async function getCookieData(cookieName) {
const {data} = await new Promise(resolve => {
window.onmessage = (event) => {
resolve(event);
}
});
var cookieContent = JSON.parse(data);
var cookieContentData = JSON.parse(cookieContent.data);
return cookieContentData; //this returns the cookie data (in almost all cases)
}
Run Code Online (Sandbox Code Playgroud)
我没有如何正确使用诺言将其移交给我的初始触发功能。我将不胜感激。
postmessage ×10
javascript ×6
iframe ×4
html5 ×3
add-on ×1
angularjs ×1
async-await ×1
c# ×1
c++ ×1
contextmenu ×1
firefox ×1
https ×1
jquery ×1
mouse ×1
oauth-2.0 ×1
promise ×1
safari ×1