javascript 如何在 HTML 标签中使用 chrome.extension.geturl() 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14806427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use chrome.extension.geturl() method in HTML tags?
提问by Arjun Vachhani
I want to load a local image from extension. I write the following code to load image. But it can not load image.
我想从扩展加载本地图像。我编写了以下代码来加载图像。但它无法加载图像。
$('.aAU').append('<div id="DivScroll"><img id="imgSlider" width="25px" src=chrome.extension.getURL("images/slider.png") /></div>');
The file is also present at that location.
该文件也存在于该位置。
When I use developer tool it shows img tag as follows
当我使用开发人员工具时,它显示 img 标签如下
<img id="imgSlider" src="chrome.extension.getURL('images/slider.png')">
If I use static path such as follows then it display the image
如果我使用如下所示的静态路径,则它会显示图像
<img id="imgSlider" src="chrome-extension://ccpdgakpdgiklccnpnihmidbjnacgkch/images/slider.png">
provide any solution to use chrome.extension.getURL()
method in HTML tags.
提供chrome.extension.getURL()
在 HTML 标签中使用方法的任何解决方案。
回答by Sudarshan
Are your resources registered in manifest file as shown here
您的资源是否已在清单文件中注册,如下所示
{
...
"web_accessible_resources": [
"images/my-awesome-image1.png",
"images/my-amazing-icon1.png",
"style/double-rainbow.css",
"script/double-rainbow.js"
],
...
}
These resources would then be available in a webpage via the URL chrome-extension://[PACKAGE ID]/[PATH]
, which can be generated with the chrome.extension.getURL
method.
然后,这些资源将通过 URL 在网页中可用,该 URLchrome-extension://[PACKAGE ID]/[PATH]
可以使用该chrome.extension.getURL
方法生成。
Did you try to set src to following code through Java Script instead of
您是否尝试通过 Java Script 而不是将 src 设置为以下代码
<img id="imgSlider" src="chrome.extension.getURL('images/slider.png')">
<img id="imgSlider" src="chrome.extension.getURL('images/slider.png')">
回答by Bilal Naqvi
'<img id="imgSlider" width="25px" src='+chrome.extension.getURL("images/slider.png")+' />'
try this it should do the trick
试试这个它应该可以解决问题