Mozilla Addon Development - 在具有不同域的窗口之间进行通信(Mozilla Addon Development - Communicating between windows with different domains)
我正在尝试创建一个插件,允许用户随意查询字典网站并查看所选单词的定义。 我一直在努力寻找一种方法来在页面工作者之间进行通信,我必须访问字典站点条目的DOM和用户正在查看的主页面。 我知道页面工作者能够从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"); } }); }); } });
define.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);
I am trying to create an addon that will allow the user to query a dictionary site at will and view the definition of a chosen word. I have been struggling to find a way to communicate between the page-worker that I have to access the DOM of the dictionary site entry and the main page that the user is viewing. I know that the page-worker is able to scrape the definition from the DOM, as I am able to see the definition logged to the console. I am having issues getting postMessage and onMessage to cooperate. I am currently attempting to bridge the gap using iframes, though other approaches are welcome.
Here are some bits of my code...
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"); } }); }); } });
define.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);
原文:
最满意答案
并非所有运算符都可以使用非成员函数进行重载。
[]
和=
是两个这样的运算符。 它们只能作为成员函数重载。有关详细信息,请参见http://en.cppreference.com/w/cpp/language/operators 。
@R Sahu's link was useful, showing that [] and = can no be declared as non-member, but it didn't really explain why. @Baum mit aguen's link cleared some other questions too. (Thanks for the information)
So, I adjusted my code to this new information as follows:
Block.h
class Block { public: //... coords* operator[](size_t); Block operator=(Block); //... };
Block.cpp
//... coords* Block::operator[](size_t index) { if(index >= 0 && index < block.size()) return &block.at(index); coords *tmp = new coords(-1, -1); return tmp; } Block Block::operator=(Block b2) { block.empty(); block.reserve(b2.block.size()); append(b2.block); return *this; } //...
This way you can call
*(*b1)[0] = c1;
beingBlock* b1
andcoords c1
.The friend modifier is useful for other types of implementations, although I only realized after that the implementation of inline had to be done in the header file, not on the cpp. Block.h
class Block { public: //... friend std::ostream& operator<<(std::ostream&, const Block&); friend std::ostream& operator<<(std::ostream&, Block&); //... }; inline std::ostream& operator<<(std::ostream& out, const Block& b) { // do something return out; }; inline std::ostream& operator<<(std::ostream& out, Block& b) { // do something return out; };
In this case, you need to pass the "this" parameter has to be passed to the function also as these are non-member functions and should be implemented outside the class, in the header file.
I hope this helps, good coding everyone.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
错误消息是相当自我解释的,您使用语法将参数传递给类的基类的构造函数,但您使用的类名不是当前类的基类。 要初始化类成员,请使用成员的名称而不是其类: Project::Project(char projectName, int sDay, int sMonth, int sYear, char supervisorName) : statrtDate(sDay, sMonth, sYear), : supervisor(supervisorName) { setProject(pro ...
-
错误:非静态成员引用必须与特定对象相关(error:a nonstatic member reference must be relative to a specific object)[2023-04-05]
首先,由于该函数是static ,因此它无权访问view 。 那是因为view是Shapemaker的非静态成员,因此只与特定的实例o Shapemaker相关联。 view需要是static否则Create函数不应该是static的。 另一种选择是view不应该是成员,应该在Create函数内Create 。 此外,枚举常量的名称在CDrawView类的范围内,并通过类名访问,如下所示: if(view.current_shape == CDrawView::line) 这个. operator用于访问 ... -
您只能初始化一次对象。 之后你只能分配它们: RandUniform::RandUniform() : downlimit(0), uplimit(1) { mean = 0.5; sigma = sqrt(1./12); } 或者,您可以将基类变量的初始化委托给基类构造函数: //Make it protected if you do not want end user to see it. RandomVar::RandomVar(double m, double s) : mean ...
-
这将有效: // // Inside .cpp file // class MyClass { static int staticMember; }; int MyClass::staticMember; This will work: // // Inside .cpp file // class MyClass { static int staticMember; }; int MyClass::staticMember;
-
Qt非静态成员引用必须与特定对象相关(Qt a nonstatic member reference must be relative to a specific object)[2022-04-16]
我认为emitResult是emitResult的非静态成员函数。 然而,你在没有任何实例的情况下从子类IpResolver::ResolvedInfo调用它。 请记住,仅仅因为ResolvedInfo是一个子类,不会以任何其他方式使它变得特殊。 具体来说,如果它不包含对父类实例的引用,它将不会按照您期望的方式工作。 有两种常用方法可以解决您的问题: 您可以IpResolver的引用IpResolver给ResolvedInfo构造函数,并在ResolvedInfo实例中保留引用: class IpReso ... -
C ++非静态成员引用(C++ nonstatic member reference)[2024-03-14]
不,错误是在谈论一个敌人的物体。 要调用一个非静态的Enemy方法,你必须有一个Enemy对象。 Enemy en; en.enemyAttack(character); 想想,当你写作 Enemy::enemyAttack(character); 哪个敌人是攻击? 没有一个。 No, the error is talking about an Enemy object. To call a non-static Enemy method you must have an Enemy object. E ... -
这是正确的语法,但构造函数委托不支持,直到C ++ 11。 Visual Studio 2012并不意味着实现C ++ 11标准。 构造函数委托是那些不支持的东西之一。 This is the correct syntax, but constructor delegation is not supported until C++11. Visual Studio 2012 does not purport to implement the C++11 standard. Constructor deleg ...
-
并非所有运算符都可以使用非成员函数进行重载。 []和=是两个这样的运算符。 它们只能作为成员函数重载。 有关详细信息,请参见http://en.cppreference.com/w/cpp/language/operators 。 @R Sahu's link was useful, showing that [] and = can no be declared as non-member, but it didn't really explain why. @Baum mit aguen's link ...