处理和中止 Firefox 扩展的下载
我的扩展程序需要奇怪的行为:)
当用户输入 URL 或单击指向某个文档的链接时,我需要向他显示一些网页而不是下载此文件(例如 pdf 的 Web 查看器)。换句话说,我希望在内容的 mimetype 和操作之间进行绑定。
有什么办法可以从特权 XUL 代码中做到这一点吗?
附言。我知道我可以编写用于在浏览器中显示内容的插件,例如 Adobe Reader 插件,但我更喜欢用 JS 而不是 C++ 编写(并且不想为插件应该工作的所有平台交叉编译我的代码)。
I need strange behavior for my extension :)
When user enters URL or clicks on link that points to some document I need show him some web page instead of downloading this file (web viewer for pdf, for example). Another words, I want to have binding between content's mimetype and action.
Is there any way to do that from privileged XUL code?
PS. I know that I can write plugin for displaying content in browser, like Adobe Reader plugin, but I prefer writing in JS instead of C++ (and don't wanna cross-compile my code for all platforms where plugin should work).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以向类别管理器注册一个实现 nsIRUIContentListener 接口的组件。该类别是
external-uricontentlisteners
。该条目是您要注册的 MIME 类型。该值是您组件的合约 ID。或者,可以直接使用 URI 侦听器注册组件,但这仅在您已在启动时加载组件时才有用。
当您的用户单击指向使用该 MIME 类型提供的文档的链接(并且没有已安装的插件已经处理该类型)时,您的组件将被创建。将调用
isPreferred
或canHandleContent
方法之一;您应该验证内容类型是否是您想要的类型,然后返回 true。然后将调用您的 doContent 方法,您可以使用它打开一个窗口来处理请求。您应该返回 true 以表明您实际上并未为现有窗口提供内容。编辑:
如果您想读取文档并就地输出另一个文档,则需要注册一个流转换器。这是通过注册一个组件来完成的,该组件使用合约 ID
@mozilla.org/streamconv;1?from=&to=来实现
。我不是 100% 确定,但您可能必须将nsIStreamConverter
接口层次结构。to
设置为*/*
,然后您的GetContentType
方法应该返回实际的您提供的内容类型。然后,您的asyncConvertData
方法将传递给目标流侦听器。数据将通过基本的 nsIStreamListener 接口提供给您,然后您可以将转换后的数据提供给目标流。You can register a component that implements the
nsIRUIContentListener
interface with the category manager. The category isexternal-uricontentlisteners
. The entry is the MIME type that you want to register. The value is your component's contract ID.Alternatively it is possible to register a component directly with the URI listener but this is only useful if your are already loading your component at startup.
When your user clicks on a link to a document served with that MIME type (and there are no installed plugins already handling that type) then your component will be created. One of the
isPreferred
orcanHandleContent
methods will be called; you should verify that the content type is the one you want and then return true. YourdoContent
method will then be called and you can use this to open a window to handle the request. You should return true to indicate that you are not actually providing content for the existing window.EDIT:
If you want to read the document and output another document in-place, you need to register a stream converter instead. This is done by registering a component that implements the
nsIStreamConverter
interface hierarchy with the contract ID@mozilla.org/streamconv;1?from=<MIME>&to=<MIME>
. I'm not 100% sure but you may have to set theto
to be*/*
and then yourGetContentType
method should return the actual content type you provide. YourasyncConvertData
method will then be passed the destination stream listener. Data will be made available to you via the basensIStreamListener
interface and you can then make the converted data available to the destination stream.