gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:30:34 25 4
gpt4 key购买 nike

我正在尝试创建一个插件,允许用户随意查询词典站点并查看所选单词的定义。我一直在努力寻找一种方法来在我必须访问字典站点条目的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com