简体   繁体   English

创建的iframe与扩展程序,Google Chrome扩展程序之间的通信

[英]Communication between created Iframe and extension, google chrome extension

I try to send message from an iframe loaded from my extension to my extension (background script or content script). 我尝试将消息从从我的扩展程序加载的iframe发送到我的扩展程序(后台脚本或内容脚本)。 The created Iframe is loaded from the extension via a content script. 通过内容脚本从扩展程序中加载创建的Iframe。 I am searching for a way to communicate but all my attempts failed... 我正在寻找一种交流的方式,但是所有尝试都失败了...

Manifest.json 的manifest.json

{
"author": "***********",
"background": {
    "page": "back/background.html",
    "persistent": true
},
"browser_action": {
    "default_title": "***",
    "default_popup": "./popup/popup.html"
},
"content_scripts": [
    {
        "matches": ["<all_urls>"],
        "js": ["./content/allurls.js"],
        "all_frames":true
    },
    {
        "matches": ["<all_urls>"],
        "js": ["./banner/confirm_banner.js"]
    }
],
"web_accessible_resources": [
    "frame.html"
],
"description": "oui",
"manifest_version": 2,
"name": "***",
"permissions": ["tabs"],
"version": "1.0"
}

Confirm_banner.js (load the iframe) Confirm_banner.js(加载iframe)

var extensionOrigin = 'chrome-extension://' + chrome.runtime.id;

window.onload = load_iframe();

function load_iframe()
{
if (!location.ancestorOrigins.contains(extensionOrigin)) 
{
    var iframe = document.createElement('iframe');
    iframe.src = chrome.runtime.getURL('../frame.html');
    iframe.style.cssText = 'position:fixed;top:0;left:0;display:block;' +
                           'width:100%;height:40px;';
    document.body.appendChild(iframe);
}
}

Frame.js (script linked with frame.html) Frame.js(与frame.html链接的脚本)

$(document).ready(function()
{
    $('#jamais').click(function()
    {
        send_message("BANNER", "jamais");
        alert("send");
    });
});

function send_message(type, data)
{
    var msg = {
        type: type,
        data: data
    };
    window.postMessage(msg, "*");
}

Handler in allurls.js (content script) allurls.js中的处理程序(内容脚本)

window.addEventListener('message', function(event) {
    if (event.data.type && (event.data.type === 'BANNER'))
    {
        alert("ouimonsieur");
    }
});

So the message from iframe.js is well sent (prooved by the alert) but the content script recieve nothing from it, even before the : 因此,来自iframe.js的消息已经很好地发送了(由警报提示),但是内容脚本甚至在之前都没有收到任何消息:

if (event.data.type && (event.data.type === 'BANNER'))

Can someone see what is wrong or what other message passing protocol i can use (i also tried with top.window.postmessage) ? 有人可以看到什么地方出了问题或者我可以使用哪些其他消息传递协议(我也尝试过top.window.postmessage)?

Ty wOxxOm for the answer, i was close : Ty wOxxOm的答案,我很接近:

just replace window by parent in frame.js and all works perfectly. 只需在frame.js window替换为parent frame.js并且一切正常。

Because even if the content script run in iframe, 因为即使内容脚本在iframe中运行,

Frame.js is not a content script, it's a iframe script and runs in the context of the extension. Frame.js不是内容脚本,而是iframe脚本,可在扩展程序的上下文中运行。

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

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