如何在最新的 Firefox WebExtension 中放弃 Xrays 视觉?
How to waive Xrays vision in latest Firefox WebExtension?
我正在按照指南进行操作 here:
我想放弃 iframe 的 Xray 视觉:
var foo = $(Components.utils.waiveXrays($("#foobar").get(0).contentWindow.document));
上面是内容脚本中的 运行 manifest.json,如下所示:
"content_scripts": [
{
"matches": /* something */,
"css": ["content.css"],
"js": ["jquery-3.2.1.js","content.js"]
}
]
但是我会得到一个未定义的对象错误:
[firefox/index.js][debug] Firefox stderr: JavaScript error: , line 0: Error: Components.utils is undefined
我认为 Mozilla 网站上的指南已经过时。它不适用于纯 WebExtension 实现。
现在正确的最新方法应该是什么?
你并不意味着能够在 firefox webextensions 中放弃 xray,如果你能够那么它是一个很快就会被禁用的错误。
I think the guide on Mozilla website is outdated.
确实。
What should be the correct latest method now?
你想做的事(即放弃 xray 包装)是不可能的。可能最好考虑一种新方法来实现你想要的,不管那是什么(我无法通过描述来判断)。
经过几天的研究(并在 mozilla firefox 邮件列表中询问)。我已经找到了解决方案。正确的指南是 here(截至 2017 年 5 月 14 日):
In the SDK, content scripts can share objects with page scripts, using
techniques like unsafeWindow and createObjectIn. In WebExtensions, the
unsafeWindow is available via wrappedJSObject instead. All the export
helper functions are available, too.
如果我想访问 window 对象的豁免 xrays 版本,我应该使用:
window.wrappedJSObject
如果页面中有 iframe,我想访问其中的对象。这是其中一种方式:
document.getElementById("the_iframe").contentWindow.wrappedJSObject
但是,我发现如果 iframe 跨域,wrappedJSObject 将无法访问(从 Firefox 版本 51.0.1 开始)。我需要寻求另一种方法如下:
提供注入所有子 iframe 的内容脚本,然后实现在子 iframe 和首页之间提供 messaging bridge 的后台脚本:
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["*://*.something"],
"css": ["content.css"],
"all_frames": true, /* ADD THIS LINE */
"js": ["jquery-3.2.1.js","content.js"]
}
]
在 content.js 中,做这样的事情:
if(window.top == window.self) { // main
main();
}
else if(window.name == "frameA") { // match frameA
browser.runtime.onMessage.addListener(function(msg) {
/* Check the message msg and then access the waived X rays vision by window.wrappedJSObject */
if(msg matches something) {
var ret = window.wrappedJSObject.foobar(msg.data);
/* Send back the return value to background.js by browser.runtime.sendMessage */
browser.runtime.sendMessage({"returnVal": ret, "from": "frameA"});
}
}
});
}
在background.js中做一些消息转发:
function forward_to_all(r)
{
var forwardMessage = function(tabs) {
for(let tab of tabs) {
browser.tabs.sendMessage(tab.id,r);
}
}
browser.tabs.query({currentWindow:true, active:true}).then(forwardMessage);
}
browser.runtime.onMessage.addListener(function(msg) {
if(msg matches something) {
forward_to_all(msg);
}
});
我正在按照指南进行操作 here:
我想放弃 iframe 的 Xray 视觉:
var foo = $(Components.utils.waiveXrays($("#foobar").get(0).contentWindow.document));
上面是内容脚本中的 运行 manifest.json,如下所示:
"content_scripts": [
{
"matches": /* something */,
"css": ["content.css"],
"js": ["jquery-3.2.1.js","content.js"]
}
]
但是我会得到一个未定义的对象错误:
[firefox/index.js][debug] Firefox stderr: JavaScript error: , line 0: Error: Components.utils is undefined
我认为 Mozilla 网站上的指南已经过时。它不适用于纯 WebExtension 实现。
现在正确的最新方法应该是什么?
你并不意味着能够在 firefox webextensions 中放弃 xray,如果你能够那么它是一个很快就会被禁用的错误。
I think the guide on Mozilla website is outdated.
确实。
What should be the correct latest method now?
你想做的事(即放弃 xray 包装)是不可能的。可能最好考虑一种新方法来实现你想要的,不管那是什么(我无法通过描述来判断)。
经过几天的研究(并在 mozilla firefox 邮件列表中询问)。我已经找到了解决方案。正确的指南是 here(截至 2017 年 5 月 14 日):
In the SDK, content scripts can share objects with page scripts, using techniques like unsafeWindow and createObjectIn. In WebExtensions, the unsafeWindow is available via wrappedJSObject instead. All the export helper functions are available, too.
如果我想访问 window 对象的豁免 xrays 版本,我应该使用:
window.wrappedJSObject
如果页面中有 iframe,我想访问其中的对象。这是其中一种方式:
document.getElementById("the_iframe").contentWindow.wrappedJSObject
但是,我发现如果 iframe 跨域,wrappedJSObject 将无法访问(从 Firefox 版本 51.0.1 开始)。我需要寻求另一种方法如下:
提供注入所有子 iframe 的内容脚本,然后实现在子 iframe 和首页之间提供 messaging bridge 的后台脚本:
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["*://*.something"],
"css": ["content.css"],
"all_frames": true, /* ADD THIS LINE */
"js": ["jquery-3.2.1.js","content.js"]
}
]
在 content.js 中,做这样的事情:
if(window.top == window.self) { // main
main();
}
else if(window.name == "frameA") { // match frameA
browser.runtime.onMessage.addListener(function(msg) {
/* Check the message msg and then access the waived X rays vision by window.wrappedJSObject */
if(msg matches something) {
var ret = window.wrappedJSObject.foobar(msg.data);
/* Send back the return value to background.js by browser.runtime.sendMessage */
browser.runtime.sendMessage({"returnVal": ret, "from": "frameA"});
}
}
});
}
在background.js中做一些消息转发:
function forward_to_all(r)
{
var forwardMessage = function(tabs) {
for(let tab of tabs) {
browser.tabs.sendMessage(tab.id,r);
}
}
browser.tabs.query({currentWindow:true, active:true}).then(forwardMessage);
}
browser.runtime.onMessage.addListener(function(msg) {
if(msg matches something) {
forward_to_all(msg);
}
});