How to show images in other domains / Chrome Packaged Apps

时光怂恿深爱的人放手 提交于 2019-12-03 22:14:18

You can request external images using XMLHttpRequest and transform them into ObjectURLs. Then set the src attribute in the <img> tag to each ObjectURL and it should work.

Since this is a very common use case, we created a library to simplify it. Just drop the apps-resource-loader ral.min.js to your project and then:

var remoteImage, 
    container = document.querySelector('.imageContainer'),
    toLoad = { 'images': [ 
       'http://myserver.com/image1.png', 
       'http://myserver.com/image2.png' ] }; // list of image URLs

toLoad.images.forEach(function(imageToLoad) {
      remoteImage = new RAL.RemoteImage(imageToLoad);
      container.appendChild(remoteImage.element);
      RAL.Queue.add(remoteImage);
});
RAL.Queue.setMaxConnections(4);
RAL.Queue.start();

Remember that you need permission in the manifest.json to all domains you will be XHR'ing to. If you don't know beforehand where those images will be hosted, you can ask permission for any url:

    permissions: ['<all_urls>'],

For other usages and to get the full library, please see the project page: https://github.com/GoogleChrome/apps-resource-loader

and a simple demo at: https://github.com/GoogleChrome/apps-resource-loader/tree/master/demo

Google has added a browser tag that allows you to include images, but so far I haven't been able to get it to work.

Here is what they show as an example

<browser src="http://i.stack.imgur.com/dmHl0.png">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!