Making content accessible on Addon SDK

2020-07-19 19:15发布

问题:

I am developing an addon using Firefox's Addon SDK (v. 1.11). My extension dynamically creates an iframe on each website and then loads an html file which includes other resources such as images, font files, etc. from the add on's local directory.

Problem

When loading any of such local resources (i.e.: "resource://" schema), the iframe fails to display them and a message is thrown:

Security Error: Content at http: //www.XXX may not load or link to resource://XXX

This is a security measure introduced on Firefox 3. When developing without the Addon SDK, the way around it is declaring a directory with "contentaccessible=yes", making the directory's contents accessible to anyone, including my add on. However, I have not been able to find similar functionality using the Addon SDK. Is there a better way of using local data on an iframe that my addon creates and inserts into a page?

回答1:

I don't think you can directly load an iFrame that points to a resource inside your URL. The browser complains because it's either breaking same origin policy or cross site scripting one. I can't remember which one right now.

if it is html content you want to load you can always inject it into the DOM and then send a message to the document object using the events API to display your custom html. I've done this in the past and it works.

so from main.js send a message to content script which will then inject your iframe html into the DOM and then you can send the document object a message to display it.

I hope this helps.



回答2:

Not sure if this was the case when you posted the question, but it appears that "resource://" should no longer be used with the Addon SDK.

If you're using the resource inside of an HTML file in the extension, you can reference it locally, otherwise you should use data.url('whatever.jpg') and pass around that value as needed.

Full info is here: http://blog.mozilla.org/addons/2012/01/11/sdk-1-4-known-issue-with-hard-coding-resource-uris/