Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firefox addon 将信息发送到上下文菜单的内容脚本_Firefox Addon_Firefox Addon Sdk - Fatal编程技术网

Firefox addon 将信息发送到上下文菜单的内容脚本

Firefox addon 将信息发送到上下文菜单的内容脚本,firefox-addon,firefox-addon-sdk,Firefox Addon,Firefox Addon Sdk,我看到了很多关于上下文菜单和双向交流的问题,似乎我知道我的问题的答案。。。“你不能”,但我还是要试试 在每个页面上都有一个由页面mod创建的模式div。此模式设计为当用户将鼠标悬停在文本节点中的单词上以提供该单词的翻译时显示。这是完美的作品,我没有任何问题与页面修改 我现在想做的是允许用户突出显示一个文本选择,右键单击打开上下文菜单,我的新菜单项将显示为“Translate selection”,然后在modal div中显示选择。问题从这里开始。我可以响应上下文并单击内容脚本中的事件,如果我不

我看到了很多关于上下文菜单和双向交流的问题,似乎我知道我的问题的答案。。。“你不能”,但我还是要试试

在每个页面上都有一个由页面mod创建的模式div。此模式设计为当用户将鼠标悬停在文本节点中的单词上以提供该单词的翻译时显示。这是完美的作品,我没有任何问题与页面修改

我现在想做的是允许用户突出显示一个文本选择,右键单击打开上下文菜单,我的新菜单项将显示为“Translate selection”,然后在modal div中显示选择。问题从这里开始。我可以响应上下文并单击内容脚本中的事件,如果我不必进行翻译,这很好。转换由web服务完成,内容脚本无法调用web服务,因为回调不存在于内容脚本的上下文中,因为它位于代理沙箱中。这意味着所有web服务调用都需要来自main.js(这就是它在页面mod中的工作方式)。问题是main.js中的上下文菜单对象没有访问DOM的权限来更新modal div的内容并显示它,它不能向内容脚本发送信息,以便内容脚本可以更新DOM并显示modal div。那么,如何从上下文菜单的附加脚本获取到DOM的翻译呢

我想用SDK做什么是可能的,还是我必须放弃很多小时的工作才能将我的项目恢复到“老派”的工作方式,这样我才能让上下文菜单正常工作

这就是我所拥有的(页面模块工作,需要上下文菜单的帮助):

exports.main=函数(选项、回调){
"严格使用",;
var myAppMenuItem,
myAppContextMenu,
myAppPanel,
myAppMod,
self=需要(“self”),
contextMenu=require(“上下文菜单”);
myAppMenuItem=require('menuitems')。Menuitem();
if(myAppMenuItem.getAttribute('checked')='false'){
返回;
}
myAppMod=require('page-mod');
myAppMod.PageMod({
包括:“*”,
contentScriptWhen:'准备就绪',
contentScriptFile:[self.data.url('jquery-1.7.2.min.js'),self.data.url('myAppmod.js'),
contentStyleFile:self.data.url('myAppmod.css'),
onAttach:功能(工作程序){
工人港(
“翻译”,
函数(数据){
require('请求')
.请求({
网址:'http://api.microsofttranslator.com/V2/Ajax.svc/Translate',
内容:{
appid:'myappid',
收件人:data.to,
from:data.from,
text:data.text
},
未完成:功能(响应){
emit('translation',{response:response.text,elementId:data.elementId});
}
})
.get();
}
);
}
});
myAppContextMenu=contextMenu.Item({
标签:“翻译选择”,
上下文:contextMenu.SelectionContext(),
contentScriptFile:[self.data.url('jquery-1.7.2.min.js'),self.data.url('myAppcontextmenu.js'),
onMessage:函数(数据){
require('请求')
.请求({
网址:'http://api.microsofttranslator.com/V2/Ajax.svc/Translate',
内容:{
appid:'myappid',
收件人:data.to,
from:data.from,
text:data.text
},
未完成:功能(响应){
}
})
.get();
}
});
};

谢谢你来到瓦拉迪米尔!以下代码符合我的要求:

在上下文菜单的main.js中:

exports.main = function (options, callbacks) {
    'use strict';
    var myAppMenuItem,
        myAppContextMenu,
        myAppPanel,
        myAppMod,
        self = require('self'),
        contextMenu = require('context-menu');

    myAppMenuItem = require('menuitems').Menuitem();

    if (myAppMenuItem.getAttribute('checked') === 'false') {
        return;
    }

    myAppMod = require('page-mod');
    myAppMod.PageMod({
        include: '*',
        contentScriptWhen: 'ready',
        contentScriptFile: [self.data.url('jquery-1.7.2.min.js'), self.data.url('myAppmod.js')],
        contentStyleFile: self.data.url('myAppmod.css'),
        onAttach: function (worker) {
            worker.port.on(
                'translate',
                function (data) {   
                    require('request')
                        .Request({
                            url: 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate',
                            content: {
                                appid : 'myappid',
                                to : data.to,
                                from : data.from,
                                text : data.text
                            },
                            onComplete: function (response) {
                                worker.port.emit('translation', { response : response.text, elementId : data.elementId });
                            }
                        })
                        .get();
                }
            );
        }
    });

    myAppContextMenu = contextMenu.Item({
        label: "Translate Selection",
        context: contextMenu.SelectionContext(),
        contentScriptFile : [self.data.url('jquery-1.7.2.min.js'), self.data.url('myAppcontextmenu.js')],
        onMessage: function (data) {
            require('request')
                .Request({
                    url: 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate',
                    content: {
                        appid : 'myappid',
                        to : data.to,
                        from : data.from,
                        text : data.text
                    },
                    onComplete: function (response) {
                        <what can I do here to send the information to the content script?>
                    }
                })
                .get();
        }
    });
};
myAppContextMenu = contextMenu.Item({
    label: "Translate Selection",
    context: contextMenu.SelectionContext(),
    contentScriptFile : [self.data.url('jquery-1.7.2.min.js'), self.data.url('myAppcontextmenu.js')],
    onMessage: function (data) {
        var text = require('selection').text;
        require('request')
            .Request({
                url: 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate',
                content: {
                    appid : 'myappid',
                    to : data.to,
                    from : data.from,
                    text : text
                },
                onComplete: function (response) {
                    var index,
                        tabs = require('sdk/tabs');

                    for (index = 0; index < workers.length; index += 1) {
                        if (workers[index].tab === tabs.activeTab) {
                            workers[index].port.emit('selectionTranslation', { text: text, response : response.text, leftOffset : data.leftOffset, topOffset : data.topOffset });
                        }
                    }
                }
            })
            .get();
    }
});
有一个全局
workers
数组变量定义在
exports.main
函数中,该函数由页面模块的
onAttach
函数填充,如下所示:

        workers.push(worker);

        worker.on(
            'detach',
            function () {
                var index = workers.indexOf(worker);
                if (index >= 0) {
                    workers.splice(index, 1);
                }
            }
        );

上下文菜单内容脚本仅用于查看单击上下文,您的
页面mod
内容脚本需要执行实际工作。这意味着您需要在单击菜单项时向其发送消息。唯一的问题是识别属于右侧选项卡/框架的工作人员。@WladimirPalant-感谢您的快速响应。你能给我一个例子,或者一个链接到一个例子,从附加脚本的上下文菜单向页面模块发送消息。我在PageMod对象上找不到允许我从PageMod的内容脚本之外向PageMod发送消息的方法或事件。此外,您是否能深入了解找出哪个工人背后的逻辑再次感谢您的帮助。所以我想我在这里找到了一些消息发送和工作逻辑:@WladimirPalant-ha。。我注意到你回答了上面的问题。是的。问题是你不知道哪个标签触发了上下文菜单-至少我没有看到一个明显的方式来获得它。
        workers.push(worker);

        worker.on(
            'detach',
            function () {
                var index = workers.indexOf(worker);
                if (index >= 0) {
                    workers.splice(index, 1);
                }
            }
        );