Advertisement
rAthus

[Appgyver Steroids] stocker toutes les images en local

Jan 13th, 2016
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. steroids.on('ready', function()
  2. {
  3.     var basePath = steroids.app.absoluteUserFilesPath;
  4.     function cacheImage(obj,type)
  5.     {
  6.         if ($(obj).hasClass('cacheImage_cached'))
  7.             return false;
  8.         if (type=='src')
  9.         {
  10.             var url = $(obj).attr('src').split('?')[0].split('#')[0].replace(/^[^a-z\d]*|[^a-z\d]*$/gi,'');
  11.         }
  12.         else if (type=='background')
  13.         {
  14.             var css = $(obj).css('background-image');
  15.             var url = css.replace('url(','').replace(')','').split('?')[0].split('#')[0].replace(/^[^a-z\d]*|[^a-z\d]*$/gi,'');
  16.         }
  17.         var filename = url.split('/')[url.split('/').length-1];
  18.         var ext = filename.split('.')[filename.split('.').length-1];
  19.         if (localStorage['cacheImage:'+url] && $(obj).attr('src') && $(obj).attr('src').substr(0,13)!='/cacheImages/')
  20.         {
  21.             if (type=='src')
  22.                 $(obj).attr('src',localStorage['cacheImage:'+url]);
  23.             else if (type=='background')
  24.                 $(obj).css({'background-image':'url('+localStorage['cacheImage:'+url]+')'});
  25.             /***///$('body').append('<div style="margin:10px;padding:10px;background:white;border:5px solid green;">cacheImages: setting local image type '+type+': '+localStorage['cacheImage:'+url]+'</div>');
  26.         }
  27.         if (!   ['cacheImage:'+url] && !$(obj).hasClass('cacheImage_cached') && (url.substring(0,4)=='http' || url.substring(0,4)=='locahost') && (ext.toLowerCase()=='jpg' || ext.toLowerCase()=='jpeg' || ext.toLowerCase()=='png'))
  28.         {
  29.             var fileTransfer = new FileTransfer();
  30.             var uri = encodeURI(url);
  31.             fileTransfer.download(
  32.                 uri,
  33.                 basePath+'/cacheImages/'+filename,
  34.                 function(entry)
  35.                 {
  36.                     /**/console.log('cacheImages: download complete: '+entry.name);
  37.                     /***///$('body').append('<div style="margin:10px;padding:10px;background:white;border:5px solid orange;">cacheImages: download complete: '+entry.name+'</div>');
  38.                     sessionStorage['cacheImage:'+url] = 'stored';
  39.                     localStorage['cacheImage:'+url] = '/cacheImages/'+entry.name;
  40.                 },
  41.                 function(error)
  42.                 {
  43.                     /**/console.log('cacheImages: download error source ' + error.source + '; download error target ' + error.target + '; upload error code' + error.code);
  44.                     /***///$('body').append('<div style="margin:10px;padding:10px;background:white;border:5px solid orange;">cacheImages: download error source ' + error.source + '; download error target ' + error.target + '; upload error code' + error.code+'</div>');
  45.                 },
  46.                 false
  47.             );
  48.             $(obj).addClass('cacheImage_cached');
  49.         }
  50.     }
  51.     function cacheImages()
  52.     {
  53.         $('img').each(function()
  54.         {
  55.             if (!$(this).hasClass('cacheImage_cached') && $(this).attr('src'))
  56.                 cacheImage(this,'src');
  57.         });
  58.         $('*').each(function()
  59.         {
  60.             if (!$(this).hasClass('cacheImage_cached') && $(this).css('background-image')!='none')
  61.                 cacheImage(this,'background');
  62.         });
  63.     }
  64.     setInterval(cacheImages,1000);
  65. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement