网页脚本和内容脚本和main.js(附加代码)脚本之间的通信 [英] communication between web page script and content script and main.js(Add-on code) script

查看:258
本文介绍了网页脚本和内容脚本和main.js(附加代码)脚本之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:对不起,这个例子工作,我发现了一个小错误,并在我的完整源代码中修复。
但是我仍然希望有人知道如何更好地回答这个问题,关于如何实现网页脚本和内容脚本和main.js(附加代码)脚本之间的交流,请回答我们可以从网页脚本访问localStorage并发送到附加脚本。






我找不到如何正确建立一个基本的沟通:

$ p
$ b

反之亦然
$ b

$ b

附加脚本 - >内容脚本 - >页面脚本
$ b

页面脚本作为页面脚本原来的网页脚本,它不在沙箱中。 页脚本来自网页标题:

 < head> 
< script type =text / javascriptsrc =http://www.domain.com/script.js>< / script>
< / head>

附加脚本是main.js和内容脚本

这是我知道的。



我试过这个它不起作用:
$ b $ main.js
附加脚本:

<$ (
include:*,
contentScriptFile:self.data.url(content.js),
contentScriptWhen: '准备好',
onAttach:功能(工人){

worker.port.on( message_from_content_script 功能(数据){
worker.port.emit(message_to_page_script ,mainStorage);
));


},
attachTo:[existing,top,frame]
} );

内容脚本

'pre> document.addEventListener( 'DOMContentLoaded',函数(){


self.port.emit( message_from_content_script,一些文本);


});

正如您所看到的,我迄今为止只尝试过在内容脚本和附加脚本之间进行通信,但它不工作。请有人告诉我一个如何实现的例子:
$ b

页面脚本 - >内容脚本 - >附加脚本



反之亦然

附加脚本 - >内容脚本 - >页面脚本

解决方案



或者,您可以使用 unsafeWindow ,但是有潜在的安全问题,你的扩展可能会被addons.mozilla.org(AMO)拒绝

UPDATE: Sorry, this example works, I found a minor bug and fixed in my full source code. But I still want somebody who know better then me to give a good answer to this question about how to achieve communication between web page script and content script and main.js(Add-on code) script, also please answer how we can access localStorage from web page script and send to add-on script.


I cannot find how to build correctly a basic communication between:

page script -> content script -> add-on script

and vice versa

add-on script -> content script -> page script

please notice that I understand page script as the original webpage script, it is not in a sandbox. The page script is from webpage header:

<head>
<script type="text/javascript" src="http://www.domain.com/script.js"></script>
</head>

Add-on script is main.js and content script is attached contentScriptFile from PageMod

This is what i know .

I have tried this and it is not working:

main.js add-on script:

 pageMod.PageMod({
    include: "*",
    contentScriptFile: self.data.url("content.js"),
    contentScriptWhen: 'ready',
    onAttach: function(worker) { 

        worker.port.on("message_from_content_script", function(data) { 
            worker.port.emit("message_to_page_script",mainStorage);
        });


     },
     attachTo: ["existing","top", "frame"]
  });

content script:

document.addEventListener('DOMContentLoaded', function () { 


        self.port.emit("message_from_content_script", "some text");


});

as you see I have tried so far only to make a communication between content script and add-on script, but it is not working. Please somebody show me an example of how to achieve:

page script -> content script -> add-on script

and vice versa

add-on script -> content script -> page script

解决方案

There is extensive documentation, including examples, on how to communicate between page scripts and content scripts using custom DOM events.

Alternatively, you can use unsafeWindow, but there are potential security issues, and your extension will probably get rejected from addons.mozilla.org (AMO)

这篇关于网页脚本和内容脚本和main.js(附加代码)脚本之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆