问题
How can I access firefox extension data from javascript code injected directly to the page?
I´m looking for something similar to web_accessible_resources
key in manifest.json for chrome extensions.
I´m building bootstraped (restarless) extension with addon-sdk. When I´m injecting individual scripts into the page from content script it works fine, but when I want to access the resource from page it won´t let me - error message is something like
Access to restricted URI denied.
. I can also access it from url bar. I´m using url format generated like this:
var data = require("sdk/self").data;
var url = data.url("some-resource.ext");
which gives something like resource://some_long_addon_id/addon_name/path_to_resource
回答1:
AFAIK that is not allowed.
Can you specify the type of resource you are trying to read?
If it is a script - you can load the script content into content script, and then add it using script tag by injecting it into DOM of page Script
//content script
var resourceData = self.data.load(NAME);
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.innerText = resurceData;
head.appendChild(script);
If it is text/xml/html/json - you can pass it using window.postMessage(). An example is shown here a link
You may also find it useful to write a method in Content script that listens for a message event in content script, and on receiving a message in content script from Page Script, you could post back to Page Script using window.postMessage() with the resource data
来源:https://stackoverflow.com/questions/17515055/how-can-i-access-firefox-extension-resources