javascript - Mozilla Addon Development - 在不同域的窗口之间进行通信

标签 javascript jquery firefox-addon

我正在尝试创建一个插件,允许用户随意查询词典站点并查看所选单词的定义。我一直在努力寻找一种方法来在我必须访问字典站点条目的 DOM 的页面 worker 和用户正在查看的主页之间进行通信。我知道页面 worker 能够从 DOM 中抓取定义,因为我能够看到记录到控制台的定义。我在让 postMessage 和 onMessage 合作时遇到问题。我目前正尝试使用 iframe 弥合差距,但欢迎使用其他方法。

这是我的一些代码...

index.js:

var getDefinition = "var def = document.getElementsByClassName('def-content');" +
                    "definition = def[0].textContent;" +
                    "word = document.getElementsByClassName('js-headword');" +
                    "word = word.textContent;" +
                    "self.port.emit('dialog', definition);" +
                    "var thiswin = document.getElementById('example').contentWindow;" +
                    "thiswin.postMessage(definition, '*');"

currPage = require("sdk/page-mod").PageMod({
    include: "*",
    contentScriptWhen: "ready",
    contentScriptFile: [
        data.url("jquery.js"),
        data.url("jquery-ui.min.js"),
        data.url("define.js"),
    ],
    onMessage: function(message){
        console.log("received message");
    },
    onAttach: function(worker){
        workers.push(worker);

        worker.on("message", function(definition){
            console.log("received message");
        });

        worker.port.on("dblclick", function(selected, thispage){
            newPage = require("sdk/page-worker").Page({
                contentURL: "http://dictionary.reference.com/browse/" + selected,
                contentScriptWhen: "ready",
                contentScriptFile: [
                    data.url("jquery.js"),
                    data.url("jquery-ui.min.js"),
                    data.url("iframe.js")
                ],
                contentScript: getDefinition,
                onMessage: function(message){
                    console.log("received message");
                }
            });
        });
    }
});

定义.js:

function calldictionary(definition){
    console.log("here comes calldictionary");
    console.log(definition);
    $('div#definition').text(definition);
    $('#define').dialog("open");
}

function send(){
    var selected = getSelected();
    if (selected != ""){
        var mainwin = document.getElementById('example').contentWindow;
        $('iframe#example').attr('src', 'http://dictionary.reference.com/browse/' + selected);
        self.port.emit("dblclick", selected);
    }
}

function getSelected() {
    if (window.getSelection) {
        return window.getSelection().toString();
    } else if (document.selection) {
        return document.selection.createRange().text;
    }
    return '';
}

$(window).dblclick(function() {
    send();
});

window.addEventListener("message", function(event){
    if (event.origin == "dictionary.reference.com"){
    console.log("received message");}
    }, false);

最佳答案

您将常规窗口消息传递与 content script messaging 混淆了.试试这个:

索引.js

var getDefinition = "var def = document.getElementsByClassName('def-content');" +
                    "definition = def[0].textContent;" +
                    "word = document.getElementsByClassName('js-headword');" +
                    "word = word.textContent;" +
                    "self.port.emit('dialog', definition);";

currPage = require("sdk/page-mod").PageMod({
    include: "*",
    contentScriptWhen: "ready",
    contentScriptFile: [
        data.url("jquery.js"),
        data.url("jquery-ui.min.js"),
        data.url("define.js"),
    ],
    onMessage: function(message){
        console.log("received message");
    },
    onAttach: function(worker){
        workers.push(worker);

        worker.on("message", function(definition){
            console.log("received message");
        });

        worker.port.on("dblclick", function(selected, thispage){
            newPage = require("sdk/page-worker").Page({
                contentURL: "http://dictionary.reference.com/browse/" + selected,
                contentScriptWhen: "ready",
                contentScriptFile: [
                    data.url("jquery.js"),
                    data.url("jquery-ui.min.js"),
                    data.url("iframe.js")
                ],
                contentScript: getDefinition,
                onMessage: function(message){
                    worker.postMessage(message);
                }
            });
        });
    }
});

定义.js:

function calldictionary(definition){
    console.log("here comes calldictionary");
    console.log(definition);
    $('div#definition').text(definition);
    $('#define').dialog("open");
}

function send(){
    var selected = getSelected();
    if (selected != ""){
        self.port.emit("dblclick", selected);
    }
}

function getSelected() {
    if (window.getSelection) {
        return window.getSelection().toString();
    } else if (document.selection) {
        return document.selection.createRange().text;
    }
    return '';
}

$(window).dblclick(function() {
    send();
});

self.on("message", function(message){
    console.log("received message");}
});

关于javascript - Mozilla Addon Development - 在不同域的窗口之间进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33382343/

相关文章:

javascript - 图像路径在我的 Angular 4 文件中不起作用

javascript - 尝试使用 Javascript 修复元素

jquery - Fancybox 自定义标题不工作 titleFormat 函数

javascript - Firefox for Android 扩展 : How to trigger event on every page load?

javascript - 为什么我的 Angular Directive(指令)没有在隔离范围内获取父 Controller 范围?

javascript - Angular 2 : Use results of one http call to do another

javascript - 从 JavaScript 中的字符串中删除 ASCII « OR »

无法使用 ctypes.open() 加载 dll

javascript - 当工作站被锁定时收到通知

javascript - 当用户通过 Tab 键离开网站时将所有音频静音