2013-02-23 77 views
0

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

   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函数会重复执行,我将非常感激。

回答

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的异步处理。

+0

我试过了,但没有奏效。如果您注意到,函数image_scanner在if语句中被调用,因为它返回一个布尔值。它位于image_scanner的内部,我创建了一个新的图像对象,并给它重复的onload函数。 – DragonSlayer 2013-02-23 18:01:11

+0

@DragonSlayer - 查看我添加到我的答案中的第二个问题。 'onload'是异步的(在image_scanner()''返回后调用很长时间,所以你不能像你试图做的那样同步使用它。 – jfriend00 2013-02-23 20:53:32

+0

如果我在onload处理程序中放置一个bool标志,然后在那之后放一个while循环在布尔标志由onload处理程序设置之前什么都不会做, – DragonSlayer 2013-02-24 15:45:57