經過幾天的研究(並在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.
如果我要訪問窗口對象的豁免X光版,我應該使用:
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"]
}
]
在內容.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);
}
});