javascript - Chrome 扩展程序在安装时的新标签中不显示背景图片

标签 javascript css google-chrome google-chrome-extension

<分区>

我正在制作一个 chrome 扩展程序,它可以更改新选项卡的背景图像。我第一次加载扩展时,背景图像没有改变。

这个问题也非常偶尔在长时间使用后出现(但我不确定在什么时候它不起作用,除了第一次安装)。谢谢。

ma​​in.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];
}

list .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
}

ma​​in.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>

最佳答案

如果在加载扩展时窗口已经加载,它将不起作用,因为 showNewImage() 由第 1 行中的 window.onload 触发。

showNewImage(); 第二次添加到脚本末尾,如下所示:

    document.body.style['background-image'] = 'url("' + theImages[whichImage] + '")';
    //localStorage.img = theImages[whichImage];
    }
    showNewImage();

这将在首次加载扩展时运行脚本。

关于javascript - Chrome 扩展程序在安装时的新标签中不显示背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37407252/

上一篇:javascript - 如何通过单击另一个 block 在可见窗口中显示 block

下一篇:html - 垂直滚动的天桥列表

相关文章:

javascript - Inkscape 突出显示 svg 的不同部分并在 html 中操作 svg map

javascript - 如何阻止机器人抓取我基于 AJAX 的 URL?

google-chrome - 是否需要同时在 Safari 和 Chrome 中测试网页?

javascript - Chrome 在我的主干页面上卡住 : how to debug?

javascript - 使用 StickyTableheaders 且 requirejs 未加载

javascript - 在 R 传单中添加不透明度 slider

html - 在 CSS 中放置什么作为选择器?使用 Meteor 进行账户输入

html - 如何使用 CSS 使按钮居中?

html - 使用CSS旋转从左到右依次显示信息

javascript - 什么会导致 HTML5 canvas toBlob 创建不完整的图像?