简体   繁体   English

在网页中显示来自Firefox附加组件数据目录的图像

[英]Displaying images from firefox add-on's data directory in webpage

I am writing a firefox addon and have a content script that does my DOM manipulation as I want, but I now want to display some images from my add-on's data directory in the manipulated webpage (rather than link to them on a remote server) but I'm not sure how to do it. 我正在编写一个firefox附加组件,并且有一个内容脚本可以根据需要执行DOM操作,但是现在我想在操纵的网页中显示附加组件的数据目录中的某些图像(而不是在远程服务器上链接到它们)但我不确定该怎么做。

I see that communication between the add-on module and content script should be done via port.emit, but the API says that the parameter has to be JSON serializable. 我看到附加模块和内容脚本之间的通信应该通过port.emit完成,但是API指出该参数必须是JSON可序列化的。

Has anyone done this before? 有人做过吗? I feel that I've missed something. 我觉得我错过了一些东西。

Although you can hard code the resource: uri to the data directory, it is better to construct it at runtime. 尽管您可以将resource: uri硬编码到数据目录,但是最好在运行时构造它。 This is done with the help of the @loader/options pseudo module, but a forward compatible way would be through the self module. 这是在@loader/options伪模块的帮助下完成的,但是向前兼容的方法是通过self模块。 If you don't have other reason to use the port mechanism, then you can pass the uri to the content script through the contentScriptOptions parameter. 如果没有其他理由使用port机制,则可以通过contentScriptOptions参数将uri传递给内容脚本。 Your content script can access it from self.options . 您的内容脚本可以从self.options访问它。 The following code will insert the mylogo.jpg at every page of the mozilla.org domain. 以下代码将在mozilla.org域的每个页面上插入mylogo.jpg

// var options = require("@loader/options");
// options.prefixURI + options.name + "/data/" works now but may change in the future
var pageMod = require("sdk/page-mod");
var self = require("sdk/self");

pageMod.PageMod({
  include: "*.mozilla.org",
  contentScriptOptions: {prefixDataURI: self.data.url("")},
  contentScriptWhen: "ready",
  contentScript: "var img = document.createElement('img');" +
                 "img.src = self.options.prefixDataURI + 'mylogo.jpg';" +
                 "document.body.appendChild(img);"
});

I've done this in a bootstrap addon with the contentaccessible param in the chrome.manifest here: https://gist.github.com/Noitidart/9406437 我已经在chrome.manifest中的contentaccessible参数的自举插件中完成了此操作: https ://gist.github.com/Noitidart/9406437

For addon-sdk, which it sounds like you are doing, I think do this: just drop your images into your data folder. 对于addon-sdk,听起来好像您正在做的那样,我想这样做:只需将图像放入数据文件夹即可。 Then with the self.data.url('img_name') it gives you a resource:// path, use that path in the webpages. 然后使用self.data.url('img_name')为您提供一个resource://路径,并在网页中使用该路径。 It should work, resource paths don't have the security issue Im pretty sure. 它应该工作,资源路径没有安全性问题,我很确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM