本文介绍了我怎样才能访问Firefox的扩展资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我怎样才能从JavaScript代码直接注入到页面访问Firefox的扩展数据? 我正在为manifest扩展中的manifest.json寻找类似于 web_accessible_resources 的东西。 我正在用addon-sdk构建bootstraped(restarless)扩展。当我从内容脚本注入单个脚本到页面中时,它工作正常,但是当我想从页面访问资源时,它不会让我 - 错误消息是类似于 。我也可以从网址栏访问它。我使用的格式如下: var data = require(sdk / self)。 var url = data.url(some-resource.ext); 给出类似于 resource:// some_long_addon_id / addon_name / path_to_resource 解决方案 AFAIK是不允许的。 >你可以指定你试图读取的资源的类型吗? 如果是脚本 - 你可以将脚本内容加载到内容脚本中,然后使用脚本标记通过将其注入脚本的DOM中 $ p $内容脚本 var resourceData = self.data.load (名称); var head = document.getElementsByTagName('head')[0]; var script = document.createElement('script'); script.type ='text / javascript'; script.innerText = resurceData; head.appendChild(script);如果是text / xml / html / json - 你可以使用window.postMessage()来传递它, 。 此处显示一个示例一个链接 你也可以找到它在内容脚本中编写一个方法来侦听内容脚本中的消息事件,以及从页面脚本接收内容脚本中的消息时,可以使用window.postMessage()将资源数据返回给页面脚本 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 . 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 解决方案 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 linkYou 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 这篇关于我怎样才能访问Firefox的扩展资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-19 20:02