【问题标题】:Chrome extension not showing background image in new tab on install [duplicate]Chrome 扩展程序在安装时未在新选项卡中显示背景图像 [重复]
【发布时间】:2016-05-26 04:12:01
【问题描述】:

我正在制作一个 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];
}

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

【问题讨论】:

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


    【解决方案1】:

    如果加载扩展程序时窗口已经加载,它不会工作,因为showNewImage()是由window.onload在第1行触发的。

    再次将showNewImage(); 添加到脚本的末尾,如下所示:

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

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

    【讨论】:

    • 不,仍然没有显示。我还可以做些什么?感谢您的帮助。
    • 也许,通过在清单中包含 "persistent":false (这会创建一个事件页面 - 请参阅 developer.chrome.com/extensions/event_pages),脚本不会像您希望的那样在后台连续运行。试试“持久”:真的吗?
    • 我试过了。仍然当我加载包时,它第一次没有显示图像。
    • 后台脚本完全没用,persistent 与否,因为它只包含一个其他脚本无法使用的副本 jQuery。
    • @Xan - 你是对的。 @Ron - 最后一击 - 加载图片的匿名功能包括$(document).ready(function(e) {,因此如果在加载扩展程序时文档已经准备好,则不会被触发。尝试删除$(document).ready(function(e) {
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 2018-03-23
    • 1970-01-01
    • 2015-07-04
    • 2012-03-14
    • 2011-01-25
    相关资源
    最近更新 更多