在 Firefox 中与 Web 内容(页面对象)共享插件对象(内容脚本)
Sharing addon objects (content scripts) with Web content (page objects) in Firefox
我花了好几天时间尝试将我的一个 Firefox for Android 扩展对象与我也从我的扩展打开的网页(声明为资源)共享。问题是我已经阅读了很多关于 the last year's changes 关于不安全 window 的文章,所以我尝试了一个非常小的新函数示例,但没有成功。我复制了示例,也尝试了自己的示例,但无法复制具有功能的现有对象。看,我在内容 window 中有一个非常大的对象要克隆,但我决定用一个小对象进行测试:
//From addon
var dog = {
name: 'Spike',
woof: function(){alert('woof woof!')}
};
然后我尝试将此对象复制到活动 window:
//From addon
var contentWindow = window.BrowserApp.selectedBrowser.contentWindow;
contentWindow.dog = Components.utils.cloneInto(
dog,
contentWindow,
{cloneFunctions: true}
);
之后,我尝试检查真正复制的内容:
alert(contentWindow.dog); //Shows: [object Object]
alert(contentWindow.dog.name); //Shows: Spike
alert(contentWindow.dog.woof); //Shows: undefined
所以,我可以克隆对象但不能克隆函数,即使我声明了 "cloneFunctions: true"。
我也试过创建一个空对象然后赋值函数(在我这么大的原始对象中思考了很多工作),但没有成功:
function greetme(user) {
return "cheers " + user;
}
var foo = Components.utils.createObjectIn(contentWindow,{defineAs: "foo"});
Components.utils.exportFunction(greetme, foo, {defineAs: "greetme"});
//foo is not an object in current window
所以...欢迎任何想法,我真的不知道该怎么做,因为理论和给出的例子不再适用。
提前致谢(非常)!!
https://blog.mozilla.org/addons/2014/04/10/changes-to-unsafewindow-for-the-add-on-sdk/
您的代码已经或多或少是正确的,但是,您 运行 在使用 XRay 包装器时遇到了麻烦。而且,为了让内容 window(网站)真正看到您 dog
,您还需要放弃内容 window 上的 XRay 包装器。
我在 Android Nightly 使用当前的 Firefox 测试了以下内容(抱歉,我的 Firefox 版本没有配置远程调试)。
运行 在主进程中(使用 WebIDE):
var dog = {
name: 'Spike',
woof: function () {
alert(contentWindow.document.title + "\n" + this.name + ': woof woof!');
}
};
var contentWindow = BrowserApp.selectedBrowser.contentWindow;
// Need to unwrap this, so that we can actually set properties on the
// object itself and not just the wrapper. Aka. make "dog" visible to
// the actual script.
var unsafeWindow = Components.utils.waiveXrays(contentWindow);
// Define Window.dog (on the unsafe window, so that the website code
// can actually see it).
unsafeWindow.dog = Components.utils.cloneInto(dog, contentWindow, {
cloneFunctions: true
});
然后我切换到实际标签并测试:
dog.woof();
它奏效了。
我花了好几天时间尝试将我的一个 Firefox for Android 扩展对象与我也从我的扩展打开的网页(声明为资源)共享。问题是我已经阅读了很多关于 the last year's changes 关于不安全 window 的文章,所以我尝试了一个非常小的新函数示例,但没有成功。我复制了示例,也尝试了自己的示例,但无法复制具有功能的现有对象。看,我在内容 window 中有一个非常大的对象要克隆,但我决定用一个小对象进行测试:
//From addon
var dog = {
name: 'Spike',
woof: function(){alert('woof woof!')}
};
然后我尝试将此对象复制到活动 window:
//From addon
var contentWindow = window.BrowserApp.selectedBrowser.contentWindow;
contentWindow.dog = Components.utils.cloneInto(
dog,
contentWindow,
{cloneFunctions: true}
);
之后,我尝试检查真正复制的内容:
alert(contentWindow.dog); //Shows: [object Object]
alert(contentWindow.dog.name); //Shows: Spike
alert(contentWindow.dog.woof); //Shows: undefined
所以,我可以克隆对象但不能克隆函数,即使我声明了 "cloneFunctions: true"。
我也试过创建一个空对象然后赋值函数(在我这么大的原始对象中思考了很多工作),但没有成功:
function greetme(user) {
return "cheers " + user;
}
var foo = Components.utils.createObjectIn(contentWindow,{defineAs: "foo"});
Components.utils.exportFunction(greetme, foo, {defineAs: "greetme"});
//foo is not an object in current window
所以...欢迎任何想法,我真的不知道该怎么做,因为理论和给出的例子不再适用。
提前致谢(非常)!!
https://blog.mozilla.org/addons/2014/04/10/changes-to-unsafewindow-for-the-add-on-sdk/
您的代码已经或多或少是正确的,但是,您 运行 在使用 XRay 包装器时遇到了麻烦。而且,为了让内容 window(网站)真正看到您 dog
,您还需要放弃内容 window 上的 XRay 包装器。
我在 Android Nightly 使用当前的 Firefox 测试了以下内容(抱歉,我的 Firefox 版本没有配置远程调试)。
运行 在主进程中(使用 WebIDE):
var dog = {
name: 'Spike',
woof: function () {
alert(contentWindow.document.title + "\n" + this.name + ': woof woof!');
}
};
var contentWindow = BrowserApp.selectedBrowser.contentWindow;
// Need to unwrap this, so that we can actually set properties on the
// object itself and not just the wrapper. Aka. make "dog" visible to
// the actual script.
var unsafeWindow = Components.utils.waiveXrays(contentWindow);
// Define Window.dog (on the unsafe window, so that the website code
// can actually see it).
unsafeWindow.dog = Components.utils.cloneInto(dog, contentWindow, {
cloneFunctions: true
});
然后我切换到实际标签并测试:
dog.woof();
它奏效了。