我有一个MFC应用程序,它有一个工作线程,我想要做的是将消息从工作线程发布到主GUI线程,以更新GUI上的一些状态消息.到目前为止我所做的是Registered a new window message
//custom messages
static UINT FTP_APP_STATUS_UPDATE = ::RegisterWindowMessageA("FTP_APP_STATUS_UPDATE");
Run Code Online (Sandbox Code Playgroud)
将此消息添加到对话框类的消息映射中
ON_MESSAGE(FTP_APP_STATUS_UPDATE, &CMFC_TestApplicationDlg::OnStatusUpdate)
Run Code Online (Sandbox Code Playgroud)
原型OnStatusUpdate是
afx_msg LRESULT OnStatusUpdate(WPARAM, LPARAM);
Run Code Online (Sandbox Code Playgroud)
和定义是
LRESULT CMFC_TestApplicationDlg::OnStatusUpdate(WPARAM wParam, LPARAM lParam)
{
//This function is not called at all.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
而工作线程调用代码是
void CMFC_TestApplicationDlg::OnBnClickedMfcbutton1()
{
ThreadParams params;
params.m_hWnd = m_hWnd;
params.FTPHost = "test_host";
params.FTPUsername = "test";
params.FTPPassword = "test";
AfxBeginThread(FTPConnectThread,¶ms);
}
Run Code Online (Sandbox Code Playgroud)
和Worker线程代码是
//child thread function
UINT FTPConnectThread( LPVOID pParam )
{
if(pParam == NULL)
{
return 0;
}
ThreadParams *params = …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种简洁的方法来检测浏览器中的postMessage是否支持发送和接收对象或只是字符串.我认为那里的某个人必须写了一些这样的东西,但我还没有设法找到解决方案.
我正在使用postMessage向/从WebWorker发送数据.虽然检测浏览器是否支持工作人员是直截了当的,但是检测对象是否可以通过postMessage发送已经证明更加困难.
我想写一个简单的检测功能.因此,如果浏览器支持发送使用它的对象.如果只允许字符串,我可以回退到使用JSON.stringify().我可能会将函数分配给dojo/has测试(尽管这与问题/答案无关).
其他人为解决这个问题做了什么?任何建议都会很棒,我是WebWorkers和postMessage的新手.提前致谢.
使用YouTube JavaScript/iFrame API我试图寻求视频的不同时间等.
我已经删除了一个演示:http://jsbin.com/yuliponu
var player;
window.onYouTubeIframeAPIReady = function() {
var iframe = document.createElement("iframe");
iframe.width = 400;
iframe.height = 300;
iframe.id = "youtubeIframe";
iframe.src = "https://www.youtube.com/embed/yta1WRuiupk?autoplay=1&enablejsapi=1&origin="+encodeURIComponent(window.location.protocol+"//"+document.domain)+"&playsinline=1&rel=0";
iframe.setAttribute('frameborder', '0');
iframe.setAttribute('allowFullScreen', '');
document.getElementById("video").appendChild(iframe);
player = new YT.Player('youtubeIframe');
};
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
Run Code Online (Sandbox Code Playgroud)
如果您在控制台中查看,您将看到此错误:
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided
('https://www.youtube.com') does not match the recipient window's origin
('http://jsbin.com').
Run Code Online (Sandbox Code Playgroud)
我需要这个来解决开始使用JavaScript API,但我不知道如何解决它.
更新:
我已经重新启动了我的电脑,并且没有发生错误.如果我再次看到错误,我会更新.
javascript youtube postmessage youtube-api youtube-javascript-api
我知道MessageEvent有source属性是发送消息的窗口对象.现在,如何让这些信息检查主文档中的iframe(当然还有消息到达的主文档中)是该特定消息的来源?是唯一可用的选项来检查location.href开event.source窗口对象,然后循环中的所有I帧,以检查它匹配?如果主文档中存在具有相同源URL的iframe,该怎么办?
以下 JavaScript 代码使用 HTML5 postMessage / addEventListener 函数向自身发送消息:
window.addEventListener('testMsg', function(event) {
alert('got a message'); /* Never happens. Why? */
}, false );
window.addEventListener('load', function(event) {
alert('sending message');
window.postMessage('testMsg', '*');
}, false);
Run Code Online (Sandbox Code Playgroud)
相应的小提琴:
该消息从未收到。是什么原因?
我知道这些onbeforeunload事件onunload。除非我弄错了,否则这些都不是我需要的,因为每当当前页面卸载时它们都会触发\xe2\x80\x94,这意味着它们会在页面刷新时以及用户导航离开(到另一个 URL)时触发。
我的场景是这样的。我创建了一个子窗口,用于window.open显示其他网站,用于审查目的(应用程序的用户根据各种标准审查网站)。子窗口需要频繁浏览,因此该onunload事件会频繁触发。
我想知道浏览器窗口本身何时实际关闭,以便父窗口可以报告类似以下内容:
\n\n\n\n\n“您已关闭预览窗口;要重新打开它吗?”
\n
这可能吗?
\n我有 iframe:
<iframe id="GameFrame"
sandbox="allow-scripts allow-pointer-lock"
src="https://127.0.0.1:112/games/1047/play">
</iframe>
Run Code Online (Sandbox Code Playgroud)
我的父页面位于:
https://127.0.0.1/arcade/not-hidden/space-blaster-1047
Run Code Online (Sandbox Code Playgroud)
我正在尝试向 iFrame 发布消息:
var gameIframe = $("#GameFrame");
gameIframe.get(0).contentWindow.postMessage("screenshot", "");
Run Code Online (Sandbox Code Playgroud)
但这会引发错误:
未捕获的语法错误:无法在“窗口”上执行“postMessage”:在调用“postMessage”时目标源“无效”。
其他尝试:
postMessage("screenshot", "https://127.0.0.1");
Run Code Online (Sandbox Code Playgroud)
无法在“DOMWindow”上执行“postMessage”:提供的目标源(“ https://127.0.0.1 ”)与收件人窗口的源(“null”)不匹配。
我怎样才能得到这个发布到 iFrame 的消息?
我确定这只是我的语法问题,但我正在尝试将变量发送到 iframe(供 colorbox 使用)。目前我接受两端的任何域(只是为了让它工作)。这是发送页面的js:
$(document).ready(function() {
if(window.location.hash) {
var urlimage = window.location.hash.substring(1);
targetiframe = document.getElementById('wbgallery').contentWindow;
targetiframe.postMessage(urlimage, "*");
console.log(urlimage);
}
});
Run Code Online (Sandbox Code Playgroud)
这是接收页面:
$(document).ready(function() {
window.addEventListener('message',receiveMessage);
console.log(event);
function receiveMessage(event) {
if (origin !== "*")
return;
inbound = event.data;
console.log(inbound);
}
});
Run Code Online (Sandbox Code Playgroud)
我看到了 urlimage 的控制台日志,可以看到一个事件,但没有任何入站事件。我正在使用Mozilla 的解释来尝试解决所有问题。
当我创建一个新的 Win32 应用程序时,我注意到了这个函数:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Run Code Online (Sandbox Code Playgroud)
当某处调用函数PostMessage或 SendMessage时会收到消息,我注意到函数 WndProc 可以接收消息,因为有一个函数注册它:
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SCREENCAPTURE));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SCREENCAPTURE);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
Run Code Online (Sandbox Code Playgroud)
注意: wcex.lpfnWndProc = WndProc;
我想了解 PostMessage() 的机制以及如何接收它,所以我创建了一个 C++ 控制台应用程序,看看我是否可以注册函数 WndProc,这是我的尝试代码:
LRESULT …Run Code Online (Sandbox Code Playgroud) 我遇到了一个问题,我必须从弹出窗口重定向到不同的域并向其发布消息。这是场景:-
如果我打开新的弹出窗口并发布消息,我可以接收消息,但在重定向的情况下则无法接收消息。这是我的代码:
function redirect(formData,popup) {
//popup=window ref object
popup.location.href="http://domain_two.com"
popup.postMessage(JSON.stringify(formData),
"http://domain_two.com");
}
Run Code Online (Sandbox Code Playgroud)
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event)
{
if (event.origin !== "http://domain_one.com")
return;
let data=event.data;
//....
}
Run Code Online (Sandbox Code Playgroud) postmessage ×10
javascript ×7
html ×2
iframe ×2
c++ ×1
cross-domain ×1
dojo ×1
dom-events ×1
html5 ×1
mfc ×1
popup ×1
post ×1
sandbox ×1
sendmessage ×1
web-worker ×1
window ×1
windows ×1
youtube ×1
youtube-api ×1