IFrame in content script. How to communicate with main.js?

纵然是瞬间 提交于 2019-12-12 16:01:59

问题


My Firefox add-on opens Fancybox (type: "iframe") from a content script (page-mod). In the Fancybox I show my own HTML page (my_fancybox_stuff.html) that is located in my own server.

Now, from the my_fancybox_stuff.js (javascript of my_fancybox_stuff.html), can I send a message to the add-on's main.js? Or to the content script that opened the Fancybox?

I was hoping the global 'self' object to be accessible from my_fancybox_stuff.js as it is accessible from the content script that opened the Fancybox.

I am interested to hear about any potential workarounds to get this done.

In Chrome extension this is trivial. In my_fancybox_stuff.js I can call chrome.extension.sendMessage() and it works.

Thanks in advance!

EDIT: Adding some code to clarify my case. Simplified the code to save folks' time on reading it. This is simple as hello world.

From the contents_scipt.js (injected with page-mod) I call:

$.fancybox.open(
    {
    // Some irrelevant Fancybox options hidden for clarity
    href:"http://www.mysite.com/my_fancybox_stuff.html
    type: 'iframe'
    }
);

my_fancybox_stuff.html would look something like this:

<!doctype html>
<head>
  <meta charset="utf-8">
  <title></title>
  <script src="js/jquery.js"></script>
  <script src="js/my_fancybox_stuff.js"></script>
</head>
<body>
<div id="my-wrapper">
  <div id="my-form-div">
    <form id="my-form" action="#">
      <div>
        <div><p>Name</p></div>
        <div><input id="fullname" type="text" name="fullname" value=""></div>
      </div>
      <div>
        <div><p><a id="my-button" href="#">Press this</a></p></div>
      </div>
    </form>
  </div>
</div>
</body>

In my_fancybox_stuff.js, when button ("my_button") is pressed, I want to send out a message containing the value of the input field ("fullname") and listen it in content_script.js.


回答1:


Yes, your my_fancybox_stuff.js can communicate with your add-on's content script using postMessage ().

See the documentation and examples at MDN.



来源:https://stackoverflow.com/questions/16977627/iframe-in-content-script-how-to-communicate-with-main-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!