I'm making a chrome extension which changes the background image of a new tab. The first time I load the extension the background image doesn't change.
This problem also very occasionally arises after using it for long periods of time (but I'm not sure at what times it doesn't work except on first install). Thanks.
main.js
/*THIS CODE IS FOR GETTING THE NUMBER OF IMAGES FROM PHP*/
// INCLUDE JQUERY LIBRARY: OTHERWISE THIS WON'T WORK...
// SURE YOU CAN ALSO DO ALL THESE IN PURE JAVASCRIPT, BUT WHY RE-INVENT THE WHEEL? :-)
(function ($) {
$(document).ready(function(e) {
$.ajax({
url: 'http://url.com/numberOfImages.php', // <== CHANGE URL
dataType: "HTML",
cache: false,
type: "POST",
//HANDLE THE SUCCESS CASE FOR THE AJAX CALL
success: function (data, textStatus, jqXHR) {
if(data){
localStorage.numberOfImages = data;
gotNumberOfImages = true;
}
},
//HANDLE THE FAILURE CASE FOR THE AJAX CALL
error: function (jqXHR, textStatus, errorThrown) {
console.log('The following error occurred: ' + textStatus, errorThrown);
},
//HANDLE THE EVENT-COMPLETE CASE FOR THE AJAX CALL
complete: function (jqXHR, textStatus) {
}
});
});
})(jQuery);
window.onload = function() {
showNewImage();
var str = document.getElementById("greet").innerHTML;
var res = str.replace("\'\'", " " + localStorage.name); // replace with name
document.getElementById("greet").innerHTML = res;
};
// Set up the image files to be used.
var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.
var numberOfImages;
if (navigator.onLine) { //checking if connected to internet, if it finds the user is online it'll call the images that are on the server.
for(i = 0; i<=localStorage.numberOfImages;i++){
theImages[i] = 'http://url.com/images/'+ i +'.jpg'; //NAME THE IMAGES ON THE SERVER 0.JPG, 1.JPG, 2.JPG and so on.
}
} else { //if offline, if it finds that the user is offline it'll use these images. REMEMBER TO PUT A FEW OF THESE.
theImages[0] = 'image3.jpg'
theImages[1] = 'image4.jpg'
}
var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
preBuffer[i] = new Image()
preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showNewImage(){
document.body.style['background-image'] = 'url("' + theImages[whichImage] + '")';
//localStorage.img = theImages[whichImage];
}
manifest.json
{
"name": "App name",
"description": "Add description",
"version": "1.0",
"permissions": [
"activeTab"
],
"chrome_url_overrides" : {
"newtab": "main.html"
},
"background": {
"scripts": ["jquery-2.2.3.min.js"],
"persistent": false
},
"permissions": [
"activeTab",
"https://ajax.googleapis.com/",
"storage",
"tabs",
"http://*/*",
"https://*/*"
],
"browser_action": {
"default_title": "some title"
},
"manifest_version": 2
}
main.html
<html>
<head>
<title>New Tab</title>
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="/octicons/octicons.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="jquery-2.2.3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
</head>
<body>
...
<script src = "main.js"></script>
</body>
</html>