无法在 DOMWindow 上执行 postMessage:目标来源
failed to execute postMessage on DOMWindow : target origin
我正在开发 firefox 插件。我有 2 个脚本 filler.js 和 aws.js
filler.js
document.getElementById("orderNow").addEventListener("click", function() {
var domain = 'https://www.amazon.com/';
var openWin = window.open(domain);
//message sender
var message = "WS Iron Man";
openWin.postMessage(message,domain); //sending the message
});
aws.js
window.onload = function () {
//alert('page loaded successfully'); //alert function working here
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
if (event.origin !== "http://localhost/waveapp/includes/pages/order_details.html")
return;
// alert to check function working or not
var msg = event.data;
alert(msg);
}
};
manifest.json
{
"manifest_version": 2,
"name": "Borderify",
"version": "1.0",
"description": "Copy details to amazon.com.",
"icons": {
"48": "icons/border-48.png"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["filler.js"]
},
{
"matches": ["*://*.amazon.com/*"],
"js": ["aws.js"]
}
],
"permissions": [
"clipboardRead",
"unlimitedStorage",
"storage"
]
}
filler.js 运行ning 在我的本地主机上,aws.js 运行 在 amazon.com 上。这是一个 Firefox 扩展。
当我 运行 时,我在控制台中收到错误消息
"无法在“DOMWindow”上执行“postMessage”:提供的目标来源(“https://www.amazon.com”)与收件人 window 的来源不匹配(“空')"
也收到这条消息
"在“http://localhost”””上自动授予跟踪器“https://www.amazon.com”的存储访问权限。 15=]
请帮我解决一下,我很抱歉我的英语不好
我建议你阅读Window.postMessage()。
它说,我引用:
Normally, scripts on different pages are allowed to access each other
if and only if the pages they originate from share the same protocol,
port number, and host (also known as the "same-origin policy").
window.postMessage() provides a controlled mechanism to securely
circumvent this restriction (if used properly).
所以这不是 CORS 错误,因为这些不适用于这种情况。
发生这种情况的原因有多种:
- 您实际上使用的 URL 与提供的
不同
- 您已经关闭了打开的 window(window 关闭时会将所有内容设置为空)
- 您正在
window.open
和 window.postMessage
之间做某事,这可能会改变存储在您的引用中的内容
我正在开发 firefox 插件。我有 2 个脚本 filler.js 和 aws.js
filler.js
document.getElementById("orderNow").addEventListener("click", function() {
var domain = 'https://www.amazon.com/';
var openWin = window.open(domain);
//message sender
var message = "WS Iron Man";
openWin.postMessage(message,domain); //sending the message
});
aws.js
window.onload = function () {
//alert('page loaded successfully'); //alert function working here
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
if (event.origin !== "http://localhost/waveapp/includes/pages/order_details.html")
return;
// alert to check function working or not
var msg = event.data;
alert(msg);
}
};
manifest.json
{
"manifest_version": 2,
"name": "Borderify",
"version": "1.0",
"description": "Copy details to amazon.com.",
"icons": {
"48": "icons/border-48.png"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["filler.js"]
},
{
"matches": ["*://*.amazon.com/*"],
"js": ["aws.js"]
}
],
"permissions": [
"clipboardRead",
"unlimitedStorage",
"storage"
]
}
filler.js 运行ning 在我的本地主机上,aws.js 运行 在 amazon.com 上。这是一个 Firefox 扩展。
当我 运行 时,我在控制台中收到错误消息
"无法在“DOMWindow”上执行“postMessage”:提供的目标来源(“https://www.amazon.com”)与收件人 window 的来源不匹配(“空')"
也收到这条消息
"在“http://localhost”””上自动授予跟踪器“https://www.amazon.com”的存储访问权限。 15=]
请帮我解决一下,我很抱歉我的英语不好
我建议你阅读Window.postMessage()。
它说,我引用:
Normally, scripts on different pages are allowed to access each other if and only if the pages they originate from share the same protocol, port number, and host (also known as the "same-origin policy"). window.postMessage() provides a controlled mechanism to securely circumvent this restriction (if used properly).
所以这不是 CORS 错误,因为这些不适用于这种情况。
发生这种情况的原因有多种:
- 您实际上使用的 URL 与提供的 不同
- 您已经关闭了打开的 window(window 关闭时会将所有内容设置为空)
- 您正在
window.open
和window.postMessage
之间做某事,这可能会改变存储在您的引用中的内容