Chrome扩展img.onload功能不断执行

时间:2013-02-23 15:13:27

标签: javascript image google-chrome onload

我正在编写一个Chrome扩展程序,可以屏蔽可能令人反感的内容。我正在实现的一种方法是扫描所有图像,看看有多少皮肤在显示。我创建一个新的图像对象,将crossOrigin标志设置为“”,然后创建一个onload函数,将图像绘制到画布上,从画布中读取数据,然后执行分析,为调用函数设置一个布尔标志。定义onload函数后,我将src从我的网页源列表中分配给我的图像节点。 image_scanner函数在for循环内部调用,该循环遍历网页上的每个图像节点并执行各种操作以阻止。这是我执行的最后一个操作。以下是调用image_scanner的代码:

                if (image_scanner(options.scanner_sensitivity, images[i]))
                {
               // Replace the image with a blank white image
               images[i].src = chrome.extension.getURL("replacement.png");


                }

这是image_scanner函数

    function image_scanner(sensitivity, image)
    {
       // Sensitivity is a number and image is an image node.

      // Declare a variable to count the number of skin pixels
      var skin_count = 0;

      if (image.width == 0 && image.height ==0)
      {
       // This means the image has no size and we cannot block it.
       return false;
      } // end if


      var return_value = null; // set bool flag
      // Create an HTML5 canvas object.
   var canvas = document.createElement('canvas');

      //window.alert("Created Canvas."); // used for testing.

      // Get the context for the canvas.
      var context = canvas.getContext("2d"); // This is what we actually use to draw images and pull the data from them.

      context.canvas.width = image.width; // Set the canvas width to the width of the image
      context.canvas.height = image.height; // Set the canvas height to the height of the image


      img = new Image(); // Create a new image node to circumvent cross-domain restrictions.
      img.crossOrigin = " "; // Set crossOrigin flag to ' ' so we can extract data from it.

      img.onload = function(){
          window.alert(img.src); // This always gives the same src until Chrome ends the function
          context.drawImage(this, 0,0); // Draw the image onto the canvas.

          var pixels = context.getImageData(0, 0, image.width, image.height).data;


          // Now pixels is an array where every four entries in the array is the RGBa for a single pixel.
          // So pixels[0] is the R value for the first pixel, pixels[1] is the G value for the first pixel,
          // pixels[2] is the B value for the first pixel, and pixels[3] is the a (alpha or transparency) value for the first pixel.

          // This means that pixels.length/4 is the number of pixels in the image.
          // Now we calculate the number of skin pixels we can have before blocking the image.
          var limit = ((pixels.length)/4) * (sensitivity/100);

          // Now we go through the array of pixel data, checking if each pixel is a skin colored pixel based on its RGB value (the first 3 entries for that pixel in the pixels array)
          // Each time we find a skin colored pixel, we increment skin_count and check if skin_count >= limit. If so, we return true.
          for (var i = 0; i < pixels.length; i += 4) // We go up by four since every four entries describes 1 pixel
          {

           // pixel is skin if 0 <= (R-G)/(R+G)  <= .5  and B/(R+G) <= .5 pixels[i] is the R value, pixels[i+1] is the G value, and pixels[i+2] is the B value.
           if ((0 <= ((pixels[i] - pixels[i+1])/(pixels[i] + pixels[i+1]))) && (((pixels[i] - pixels[i+1])/(pixels[i] + pixels[i+1])) <= 0.5) && ((pixels[i+2]/(pixels[i] + pixels[i+1])) <= 0.5))
           {
               skin_count++;
               //window.alert("Found skin pixel."); // used for testing.

               if (skin_count >= limit)
               {
                   //window.alert("Blocking image with src: " + image.src); // used for testing.
                   img.onload = null; // try to clear the onload function
                   return_value = true;
                   return false;
               } // end inner if
           } // end outer if
          } // end for loop

       //var temp;
       img.onload = null;
       return_value = false;
       return false;

      }; // end onload function


   img.src = image.src; // Set the new image to the same url as the old one.    



   return return_value;
} // end image_scanner

我不确定问题是什么,但是onload函数会运行,遍历像素,设置标志,返回,然后再次运行。我在Chrome的调试器中尝试过调试,这就是我能找到的。我已经尝试在onload函数中将onload设置为null,但它不起作用。我试过从onload函数返回false。我已经尝试在image_scanner函数中等待,直到return_value!= null,但这似乎进入了一个无限循环,我甚至从来没有从onload函数得到警报。如果有人知道为什么onload函数会重复执行,我将非常感激。

1 个答案:

答案 0 :(得分:0)

如果您要将.src设置为空白图片,并且在加载时不希望再次调用.onload,则应在设置{之前清除.onload {1}}。

.src

看起来你正在从if (image_scanner(options.scanner_sensitivity, images[i])) { // clear our onload handler images[i].onload = function() {}; // Replace the image with a blank white image images[i].src = chrome.extension.getURL("replacement.png"); } 处理程序返回一个值,并期望从image_scanner函数返回该值。它不起作用。在onload已经返回很久之后很久,onload处理程序会被调用一段时间。您需要重写代码以使用image_scanner的异步处理。