0

我正在编写一个阻止可能令人反感的内容的 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 函数会重复执行,我将不胜感激。

4

1 回答 1

0

如果您要设置.src为空白图像并且不想.onload在加载时再次被调用,那么您应该.onload在设置之前清除.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");
}

看起来您正在从onload处理程序返回一个值,并期望从 image_scanner 函数返回该值。它不是那样工作的。onload 处理程序在很长一段时间后被调用,很久之后image_scanner已经返回。您将需要重写代码以使用onload.

于 2013-02-23T15:22:51.453 回答