How can I access firefox extension resources

最后都变了- 提交于 2020-01-05 01:24:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!