Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- steroids.on('ready', function()
- {
- var basePath = steroids.app.absoluteUserFilesPath;
- function cacheImage(obj,type)
- {
- if ($(obj).hasClass('cacheImage_cached'))
- return false;
- if (type=='src')
- {
- var url = $(obj).attr('src').split('?')[0].split('#')[0].replace(/^[^a-z\d]*|[^a-z\d]*$/gi,'');
- }
- else if (type=='background')
- {
- var css = $(obj).css('background-image');
- var url = css.replace('url(','').replace(')','').split('?')[0].split('#')[0].replace(/^[^a-z\d]*|[^a-z\d]*$/gi,'');
- }
- var filename = url.split('/')[url.split('/').length-1];
- var ext = filename.split('.')[filename.split('.').length-1];
- if (localStorage['cacheImage:'+url] && $(obj).attr('src') && $(obj).attr('src').substr(0,13)!='/cacheImages/')
- {
- if (type=='src')
- $(obj).attr('src',localStorage['cacheImage:'+url]);
- else if (type=='background')
- $(obj).css({'background-image':'url('+localStorage['cacheImage:'+url]+')'});
- /***///$('body').append('<div style="margin:10px;padding:10px;background:white;border:5px solid green;">cacheImages: setting local image type '+type+': '+localStorage['cacheImage:'+url]+'</div>');
- }
- 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'))
- {
- var fileTransfer = new FileTransfer();
- var uri = encodeURI(url);
- fileTransfer.download(
- uri,
- basePath+'/cacheImages/'+filename,
- function(entry)
- {
- /**/console.log('cacheImages: download complete: '+entry.name);
- /***///$('body').append('<div style="margin:10px;padding:10px;background:white;border:5px solid orange;">cacheImages: download complete: '+entry.name+'</div>');
- sessionStorage['cacheImage:'+url] = 'stored';
- localStorage['cacheImage:'+url] = '/cacheImages/'+entry.name;
- },
- function(error)
- {
- /**/console.log('cacheImages: download error source ' + error.source + '; download error target ' + error.target + '; upload error code' + error.code);
- /***///$('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>');
- },
- false
- );
- $(obj).addClass('cacheImage_cached');
- }
- }
- function cacheImages()
- {
- $('img').each(function()
- {
- if (!$(this).hasClass('cacheImage_cached') && $(this).attr('src'))
- cacheImage(this,'src');
- });
- $('*').each(function()
- {
- if (!$(this).hasClass('cacheImage_cached') && $(this).css('background-image')!='none')
- cacheImage(this,'background');
- });
- }
- setInterval(cacheImages,1000);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement