XUL:如何允许从浏览器脚本访问顶部窗口(当src为localhost时)

XUL: how to allow access to top window from a browser script (when src is localhost)

本文关键字:src localhost 窗口 顶部 何允许 浏览器 访问 脚本 XUL      更新时间:2023-09-26

XUL:

<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    <browser type="chrome" disablesecurity="true" src="http://localhost:60000/test.html" flex="1" disablehistory="true" />
</window>

test.html:

<script>
    window.parent.document.title = "abc";
</script>

jsconsole中的错误:Permission denied to access property 'document'

如果我将"http://localhost:60000/test.html"更改为"chrome://test.html",那么一切都可以,但在我的情况下,我可以通过web服务器访问。

出于安全原因,您不能按照自己的意愿做任何事情。

从非chrome://地址或其他特定于Firefox扩展的协议(如resource://(加载的页面在所有正常网页(非特权(的安全上下文中运行。这明确阻止的一件事是访问当前页面之外的Firefox区域。因此,您尝试访问当前浏览器window的父级应该总是失败。

在浏览器的URL栏中使用chrome://test.html地址明确指示Firefox在其可以访问大多数Firefox的扩展的更高安全上下文(特权(中加载页面。

如果您需要从非特权文档访问chrome://资源,则可以使用自定义DOM事件来实现消息传递。然而,在特权方面,您必须之前在执行此操作的Firefox配置文件中安装了Firefox扩展。换句话说,用户必须已经通过安装扩展的过程,或者至少已经从chrome://地址访问了一些东西,来授予对特权级别的访问权限。这样的地址只能从用户本地文件存储上数量非常有限的文件夹中获得。

如果您想了解更多信息,MDN上有一个关于特权页面和非特权页面之间的交互的页面。